Skip to main content

Questions tagged [wp-query]

"wp-query" ambiguously refers to one of two things. 1) WP_Query is a class native to WordPress which allows users to query the posts database using a number of criteria. 2) $wp_query is the global variable that holds the main query object prepared by WordPress based on a given request.

463 votes
7 answers
194k views

When should you use WP_Query vs query_posts() vs get_posts()?

It seems like half the tutorials in the Codex and around the blogosphere use query_posts() and half use WP_Query. They all do similar things, so when should I use one over the others?
Dan Gayle's user avatar
  • 6,136
179 votes
5 answers
200k views

When to use WP_query(), query_posts() and pre_get_posts

I read @nacin's You don't know Query yesterday and was sent down a bit of a querying rabbit hole. Before yesterday, I was (wrongly) using query_posts() for all my querying needs. Now I'm a little bit ...
saltcod's user avatar
  • 2,098
22 votes
1 answer
8k views

Some doubts about how the main query and the custom query works in this custom theme?

I am pretty new in WordPress theme development and I'm not so into PHP (I came from Java and C#) and have the following situation in this custom theme As you can see in the homepage I first show a ...
AndreaNobili's user avatar
62 votes
13 answers
89k views

Using wp_query is it possible to orderby taxonomy?

My question is simple, I'm using WP_Query to retrieve some custom type posts filtering by a taxonomy using tax_query. Now my problem is I would like to orderby the taxonomy, but from documentation ...
yeope's user avatar
  • 787
7 votes
8 answers
59k views

Get all categories and posts in those categories

I am looking for a solution that allows me to print the following: Cat 1 Cat 2 Cat 3 Post 1 Post 1 Post 1 Post 2 Post 2 Post 2 Post 3 Post ...
Ben's user avatar
  • 233
51 votes
14 answers
81k views

meta_query with meta values as serialize arrays

I'm working on a project in which I'm creating a custom post type and custom data entered via meta boxes associated with my custom post type. For whatever reason I decided to code the meta boxes in ...
tollmanz's user avatar
  • 5,510
6 votes
1 answer
7k views

Pagination returns 404 after page 20

I got a paged loop that pulls in 4 posts per page from custom post type. It works great, with the only downside that the pagination wont go over the page/20 everything after that is 404, and the ...
any_h's user avatar
  • 360
23 votes
1 answer
2k views

Should we trust the post globals?

@toscho left a comment to this answer which had me thinking again. How much trust should we have in the global scope, specially regarding the post globals like $post? So what? The global variable ...
Pieter Goosen's user avatar
32 votes
5 answers
53k views

Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title

I'm working on a custom taxonomy term page template where we want the items that are connected to the term sorted by a publication date (custom date field) - and if there are multiple items on the ...
CSSgirl's user avatar
  • 746
27 votes
4 answers
43k views

Can i merge 2 new WP_Query($variable) 's?

I am running a multisite network and i have setup a sql query that uses swith_to_blog(); and queries the posts. Is there a way that i can declare the query inside a new WP_Query and actually merge ...
Rory Rothon's user avatar
26 votes
2 answers
16k views

Using pre_get_posts with WP_Query

I was reading Stephen Harris's excellent answer to this question regarding the use of WP_query(), query_posts() and pre_get_posts. He says "pre_get_posts is a filter, for altering any query. It is ...
Ben Pearson's user avatar
19 votes
5 answers
20k views

How to use a custom post type archive as front page?

I'd like to use a custom post type archive as a site's front page, so that http://the_site.com/ is a custom post type archive displayed according to my archive-{post-type}.php file. Ideally I ...
Isaac Lubow's user avatar
16 votes
2 answers
32k views

Combining queries with different arguments per post type

I am building a section on a site where I am merging two different post types into one loop, and then displaying them randomly. The problem is, I'm having a hard time finding a way to limit the amount ...
Andy Merskin's user avatar
9 votes
3 answers
20k views

The next_posts_link() works only with original $wp_query

I've got custom template that I want to display paged blog posts. This is the beginning of my file: $wp_query = new WP_Query($args); if($wp_query->have_posts()){ while($wp_query->...
Atadj's user avatar
  • 2,162
4 votes
1 answer
4k views

How to paginate attachments in a secondary query as gallery?

I have this code that shows all images attached to my post-type. Is there a way to paginate those? I have like 25 pics and I wan't to avoid scroll as much as I can. Code: <div class="galleries ...
artist learning to code's user avatar

15 30 50 per page
1
2 3 4 5
36