Set up The Loop with query parameters. This will override the current WordPress Loop and shouldn’t be used more than once. This must not be used within the WordPress Loop.

learn more… | top users | synonyms

198
votes
6answers
64k 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. What's the deal?
72
votes
3answers
40k 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 ...
13
votes
3answers
9k views

What are all the available parameters for query_posts?

The WordPress codex page for query_posts states that it does not cover all the parameters which query_posts takes: Parameters This is not an exhaustive list yet. It is meant to show some of ...
11
votes
4answers
7k views

what is the correct way to compare dates in a WP query_posts meta_query

I have a query_posts call in a WP template. Through the use of the More Fields Plugin I can give the site admin the ability to create an event (custom post type) and then enter a date which is ...
11
votes
1answer
3k views

Alternative to query_posts for main loop? [duplicate]

Possible Duplicate: When to use WP_query(), query_posts() and pre_get_posts I just noticed today that the documentation for query_posts() mentions some "disadvantages" of using query_posts ...
11
votes
3answers
15k views

How to Get All Posts with any post status?

i am creating a frontend dashboard where i need to show all the post by the current user. So i need to show posts with all post status. Mainly published, trashed and the pending. I am now using ...
9
votes
4answers
263 views

Is there a way to exclude the content from the post variable to save on RAM usage?

So, I've run into what looks like a WP RAM usage problem and am looking for a solution. The only place I'm really having this problem on my site is with a Site Map page that I'm trying to populate, ...
9
votes
3answers
9k views

Order by meta value or date?

Got a custom field called startDate but its only on a few events. I was wondering if it isn't set for a post I could use post_date to generate the posts list? // if meta_key _postmeta.startDate isn't ...
8
votes
6answers
11k views

Wp get all the sub pages of the parent using wp query

Here is my code $my_wp_query = new WP_Query(); $all_wp_pages = $my_wp_query->query(array('post_type' => 'page','post_parent'=>$parid,'orderby'=>'title','order'=>'ASC' )); It displays ...
7
votes
3answers
12k views

posts_per_page no limit

I want to return ALL posts with query_posts. I tried setting posts_per_page to a really high number, but query_posts freaks out and doesn't return any posts. What is the correct way to query posts ...
7
votes
2answers
4k views

How do I query by post format in WordPress 3.1

I am trying to query for all posts with a post format of 'quote.' I have added the post formats to my functions.php with add_theme_support( 'post-formats', array( 'image', 'video', 'gallery', 'quote' ...
6
votes
4answers
2k views

Display/query post formats

WP 3.1 has everyone excited, if somewhat confused, about post formats. Enabling new post formats The issue of enabling post formats has been covered at length. It's as simple as adding this line to ...
6
votes
1answer
9k views

How to query_posts using meta_query to orderby meta_key AND have a secondary sort by date?

I've been troubleshooting an issue over the past few days and keep going in circles. Could really use a fresh pair of eyes to help me answer this question... So I'm working with a wordpress site ...
6
votes
1answer
1k views

Using Wordpress to make a “Product Search” type navigation drilldown

This is somewhat of a follow-up to a question I asked some time ago: Trying to design nice category drilldown with one "held" taxonomy and a repeatable list of categories I've sort of got ...
6
votes
4answers
5k views

Querying posts from multiple sites in a network?

I realize there have been a few questions which dance around the solution I am after but I believe I am looking for something specific. This is actually a two part question: 1) My goal is to have ...
5
votes
4answers
16k views

query_post by title?

is it possible to create a loop of posts using WP_Query or query_posts using the title? ie $args = array('post_title'='LIKE '.$str.'% '); $res = WP_Query($arg); // the loop... // trying this ...
5
votes
2answers
13k views

How to call a custom post with get_posts() instead of query_posts()?

I currently use query_posts to show these custom posts but I'm pretty sure that I should uses get_posts() to write it correctly. <?php query_posts( array( 'type-mario' => 'games', 'showposts' ...
5
votes
1answer
4k views

Using meta_query, how can i filter by a custom field and order by another one?

With the following code (in functions.php) my posts (of CPT event) are ordered by _end_date instead of _start_date. What's the proper solution to this as of WP 3.1.3? Of course I'd like to avoid using ...
5
votes
1answer
4k views

Get posts from sites in Multisite?

I'm trying to pull multiple sites posts. For example, I can pull out a single site posts by a category and total posts 10. But I'm trying to pull out both posts from two separate Multisite blogs 1 ...
5
votes
3answers
949 views

How to query post by user role?

I want to query posts by their author role. And do something with the post based on the role. I know we can get the post by get_posts or WP_query, the problem is there are no argument to sort the ...
4
votes
5answers
4k views

How to return results of a get_posts() in explicitly defined order

I'm trying to create a loop of explicity ordered posts, for example: <?php $args = array( 'include' => '1,3,8,4,12' ); ?> <?php get_posts( $args ); ?> The results are ...
4
votes
4answers
6k views

query_posts exclude a meta key

<?php query_posts(array('showposts' => 1000, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC', 'meta_key' => featured_product, ...
4
votes
4answers
8k views

Using WP_Query to Query Multiple Categories with Limited Posts Per Category?

I have 3 categories with each 15 posts, I want to do ONE query to the db bringing only 5 first posts for each category, how can I do it? $q = new WP_Query(array( 'post__in' => array(2,4,8), ...
4
votes
4answers
5k views

Fail to compare dates in meta_query

This is the piece of code I use to get event posts whom date metadata is newer than today : <?php query_posts( array( 'post_type' => 'concerts', 'meta_key' => ...
4
votes
2answers
2k views

Get posts by menu ID

I'm a little stuck here. I have a menu in Wordpress with some posts in it. What I would like is to retrieve all the posts as objects. I think I'm pretty close, but the SQL query doesn't seem to work ...
4
votes
3answers
6k views

Modify main WordPress loop with a parse_query filter

I am trying to modify the main WordPress loop on my category pages like so: add_filter('parse_query', 'my_modified_query'); function my_modified_query( $q ) { if (!is_admin() && ...
4
votes
1answer
2k views

Hide other users' posts in admin panel

I intended to run a multi-author site, I don't want the posts from other authors to be shown in /wp-admin/edit.php page. I managed solve this problem by the codes from this thread. The code is like ...
4
votes
1answer
5k views

Custom Taxonomy not working with posts_per_page in new WP_query (pagination problem)

OK, so i'm messing with Custom Taxonomies for the second time and in both instances i've run into this problem where using the posts_per_page argument in either a query_posts or WP_query scenario.. ...
4
votes
4answers
5k views

How do I exclude a custom taxonomy from the post loop

Is there a simple or easy way to exclude all posts from a custom taxonomy in the loop? I've been looking high and low, and neither SE, SO or Google seem to have a straight answer. I know it can be ...
4
votes
1answer
1k views

Help to condense/optimize some working code

WordPress by default shows ALL posts to users who are Authors. I wanted Authors to only be able to see their own posts so I came up with the following code which works well. Any suggestions to ...
4
votes
2answers
586 views

mysql custom wp query

this code is designed to select posts with selected meta value in wordpress query <?php $values = $wpdb->get_results("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = ...
4
votes
2answers
4k views

query_posts() in function makes global $wp_query out of sync?

This has me stumped for a while already. Either I am missing something very obvious or something very not obvious. I am also not entirely sure if this has something to do with WP or purely PHP ...
4
votes
1answer
4k views

How to filter custom posts by tags and custom fields?

I'm planning a site for a client who wants users to be able to filter upcoming events by the following criteria: Type i.e. Comedy, Theatre, Music etc. Date Daily or Weekend Duration Daily or Half ...
4
votes
2answers
761 views

Build a content and excerpt grid loop with paging and options for # of posts

What I'd like to do: on index.php, a loop that shows a selectable number of full posts using the_content and then below that shows a selectable number (the number of posts can be hardcoded in the ...
4
votes
1answer
322 views

Limiting query_posts to 1, regardless of sticky post?

As part of my homepage template, using the standard query_posts I'm pulling out 1 post (styled using a different content_part), then an ad, then the rest of the posts. This works fine, except for when ...
3
votes
3answers
14k views

query_posts ->using meta_compare / where meta value is smaller or greater or equals

I am using query_posts( $args ) to filter the Loop. I want to filter posts based on their meta_value "vote", sometimes smaller than, sometimes equals and so on.... I definitly want to use the ...
3
votes
4answers
865 views

How do I create a random post that will last for a day

How do I create a random post but it shouldnt be change when the page is refreshed I want a random post that will last for a day and after 24 hours or a day, it will change randomly the post , ...
3
votes
3answers
6k views

Does the 'cat' argument in query_posts fetch posts from subcategories as well as the given ID?

Hello friends I am new to wordpress. I have used query like this query_posts('cat=1,2,3') now If category 1 is parent of 2 and 3. then query_posts('cat=1') and query_posts('cat=1,2,3') will get ...
3
votes
1answer
2k views

WP_Query vs get_posts

I have read a number of questions on SE regarding these but I still can't figure the exact difference wrt usage. Would it be true to say that I would probably use WP_Query for the majority of ...
3
votes
1answer
4k views

Nested meta_query with multiple relation keys

I am curious whether Wordpress is able to run nested meta_query, with each having different relation keys? As of Wordpress 3.0, tax_query is able to perform this function; I'm wondering whether this ...
3
votes
4answers
7k views

Query posts by custom taxonomy ID

I have a custom post type called portfolio and a custom taxonomy called build-type (acting as categories) I am trying to query portfolio posts by build-type ID e.g. all Portfolio posts in "Hotels" ...
3
votes
2answers
2k views

Custom post type archive 404's with paginate_links

im using paginate_links function to create pagination on my custom post type archives, no matter what i do im hitting 404 errors when going to view page 2 (ie clicking to go on one page in the ...
3
votes
3answers
17k views

How to show all posts of the category in wordpress?

I have create a category by using Custom Post Type plugin, and now only the 5 most recent posts of the category are showing. What I want is to show all posts for the category. For examle, suppose I ...
3
votes
2answers
3k views

Sort posts alphabetically by custom field value, insert divider between different letters

i'm listing all posts of my custom post type "person" alphabetically sorted by the custom field last_name on a page. How would i insert a divider (e.g. an image of the letter) before a letter range ...
3
votes
1answer
2k views

Is it better practice to use query_posts, WP_Query, or get_posts to create various custom loops within a Page?

Right now, I'm using get_posts to retrieve cusstom posts types with a custom taxonomy assigned to it in order to generate static content like this: <?php /** * Template Name: Front Page * ...
3
votes
1answer
2k views

Filtering posts by post meta data

I want to filter posts in such a way that only posts, for which get_post_meta($post->ID, "project_cat", true) (it returns a post ID) is equal to my specified value, are shown. Any way to do this? I ...
3
votes
1answer
398 views

Order posts by ID in the given order

In the back-office of my theme, the users can choose which posts to display in the front page, and they can also choose in which order the selected posts should appear. I have tried like this: ...
3
votes
2answers
506 views

Sort X categories by last update and show image

I want to display X categories, and sort them by last update. I also want to grab the image of the latest post with the category. <img src="<?php echo ...
3
votes
5answers
23k views

how to query posts by category and tag?

I am trying to show a list of posts that are related to category X and tag Y. I've tried the following code: $args = array( 'posts_per_page' => 4, 'tag_id' => $tag_id, 'cat' => ...
3
votes
1answer
708 views

Display Posts by modifying the where clause only for my query

I am trying to display posts that are 7 days old. For that i need to modify the 'where' clause as in code //Hack found on the bottom of http://codex.wordpress.org/Template_Tags/query_posts ...