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.
148
votes
3answers
39k 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?
49
votes
3answers
18k 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 ...
11
votes
3answers
5k 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
3answers
4k 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 ...
9
votes
1answer
2k 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 ...
8
votes
4answers
206 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, ...
8
votes
3answers
7k 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 ...
8
votes
3answers
7k 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 ...
6
votes
5answers
5k 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 ...
6
votes
2answers
3k 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
1answer
953 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
1answer
6k 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 ...
5
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 ...
4
votes
4answers
5k 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
7k 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 ...
4
votes
2answers
9k views
How to call a custom post with get_posts() instead of query_posts()?
I currently use a query_posts to show theese custom posts but I'm pretty sure that I should uses get_posts() to write it correctly.
<?php query_posts( array( 'type-mario' => 'games', ...
4
votes
1answer
2k 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 ...
4
votes
2answers
1k 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
4k 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
3k 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
2answers
3k 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
3answers
450 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
2answers
431 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
140 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 ...
4
votes
4answers
4k 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 ...
3
votes
2answers
4k 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 ...
3
votes
3answers
5k views
is query_posts get child category posts?
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
3answers
510 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
1answer
1k 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
920 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:
...
3
votes
3answers
9k 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
4answers
3k 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 ...
3
votes
1answer
757 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 ...
3
votes
3answers
2k 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' => ...
3
votes
2answers
2k 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
2answers
372 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
1answer
423 views
the_author() returns empty string
I have created a page template for the front page of a site I am building using WordPress. I am displaying the 3 latest posts on this page and all works fine except for the display of the author of ...
3
votes
1answer
256 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
...
3
votes
1answer
129 views
How should I intercept the main query and inject custom join / order by / group by criteria
I have a CPT that is a member of two separate taxonomies. On the taxonomy archive page for one of the taxonomies, I need to further group and sort on the second taxonomy like so:
...
3
votes
1answer
1k views
Display only the latest post from multiple categories
I have a jQuery slider at the top of my index.php, which works fine. The only problem is that I would like to use query_posts to only display the latest post from five different categories, and always ...
3
votes
4answers
13k 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
73 views
Using Offset in Custom Post Type Query
I'm using the following query for a custom post type:
<?php
$posts = get_posts(array(
'numberposts' => -1,
'offset' => 20,
...
2
votes
4answers
2k 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 ...
2
votes
3answers
10k 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 ...
2
votes
4answers
6k 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), ...
2
votes
1answer
769 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 ...
2
votes
1answer
2k views
How can i get count from query post
How can i get the row counts in query post like mysql count(*).
$obj_name = new WP_Query($args);
while ($obj_name->have_posts()) : $obj_name->the_post();
// here i want to predict looping ...
2
votes
1answer
2k views
pre get posts changing the query
i need to change the posts in taxonomy.php page. I have a meta_key which i want to compare to meta value. But currently my code is not returning anything. I am sure i am missing something. Please let ...
2
votes
2answers
69 views
Different amount of posts on homepage than paged pages
I've done some searches here and on Google but everything I'm finding isn't working correctly. I was hoping to post my code here and get help with where I'm going wrong.
On the index page of my ...