get_posts() is a function for creating multiple loops. It retrieves a list of latest posts or posts matching criteria.

learn more… | top users | synonyms

198
votes
6answers
62k 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
39k 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 ...
6
votes
1answer
2k views

What does setup_postdata ($post ) do?

The codex defines it as 'Set up global post data. Helps to format custom query results for using Template tags.' I don't really understand this. Here is a code example : global $post; $args = ...
5
votes
3answers
28k views

Query for custom post type?

I have installed the Custom Post Type UI plugin. After activation of this plugin I have created a custom post type called portfolio. Now I want to use this on the portfolio page in the front-end. How ...
5
votes
1answer
8k views

How to get attachment file name not attachment URL

I'm using this code to list the image attachments of a post: <select name="chb_homes_for_sale_specifics_floor_plan" style="width:100%;"> <option ...
5
votes
3answers
3k views

Getting the Intersection of Two Custom Taxonomy Terms for a Custom Post Type?

(Moderators note: Was originally titled "How to get_posts of a custom post type that is in two custom taxonomies?") I have a custom post type that has two taxonomies which are separate, one called ...
5
votes
2answers
936 views

Differences Between WP_Query() and get_posts() for Querying Posts?

What are the differences between using WP_Query() and get_posts()? Which is better to use in what case and why?
5
votes
1answer
521 views

How to organize and cache additional data associated with terms?

I'm updating a plugin of mine and am have a hard time figuring out the "best way" to build in a specific bit of functionality. The plugin allows users to associate images from their media library to ...
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
1answer
5k views

Why get_posts are only showing five posts (retrieved by assigning a category to them?

Here is the link http://www.brianfunshine.com/voice-work/voice-page/ This is the code: <?php /** * Template Name: Voice Page (Two Columns) * @package WordPress * @subpackage Twenty_Ten * ...
4
votes
2answers
533 views

the_title() and the_permalink() won't work on AJAX calls

I've run into a strange problem. I have a custom loop built with get_posts that works fine when loading the page normally: <?php $rows = get_posts(array( 'post_type' => ...
4
votes
1answer
488 views

How-to exclude terms from the main query the most performant way?

This Q is a follow up to this answer on the Q: "How to exclude a specific term for the search?". 4 ways to filter out posts that have a specific term Type | Pro | ...
4
votes
2answers
240 views

Show the First Post from Each of 3 Different Post Types on a Web Page?

I have 3 different custom post types: 1.) "events", 2.) "winners" and 3.) "offers". How would I go about retrieving the first (latest) post in each of these post types on a single web page (i.e a home ...
4
votes
2answers
478 views

How to get current post ID in quick edit callback

This question is related to another previously asked question, which is a follow up on an answer. Task As I want to extend QuickEdit to be something really awesome (and more than just quick edit), I ...
3
votes
4answers
7k views

setup_postdata() does not seem to be working?

I am not sure why but I have used get_posts() to query for some data. Then I used setup_postdata() ... I think its used so that I can use functions like the_permalink() etc with the new post data? ...
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
3answers
3k views

get_children() Not Working with orderby Parameter

I am trying to use the orderby parameter in the get_children function as below: $navigation = get_children(array( 'post_parent' => $parent->ID, 'orderby' => 'menu_order' )); But it is ...
3
votes
5answers
13k views

How to get all posts related to particular category name?

I am developing one project and in this project i have to display all the posts related to particular category name. I have searched a lot but i haven't got any idea to implement this. How can i do ...
3
votes
1answer
181 views

How to get_posts having post thumbnail?

I have following code: $posts = get_posts(array( "meta_key" => "special", "meta_value" => "surprise", // "has_post_thumbnail" => true, // how to do that? )); What should I do to ...
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
2answers
193 views

How do you get posts by meta_query using the JSON API plugin?

I would like to get post with an API method using a kind of meta query. I have 2 custom fields "place" and "dayoweek" and I want to retrieve via API call all the post that have some values for this 2 ...
3
votes
4answers
2k views

Only get_posts of certain post formats

I'm trying to create a archive list with only my "normal" post format articles (not link, aside, quote, etc formats). How would I implement has_post_format( 'standard' ), or something similar, into ...
3
votes
1answer
253 views

Sanitation needed for WP_Query or get_posts calls?

Just wanted to get a straight answer on this: when submitting query_vars to a call to get_posts or WP_Query, is sanitation needed or does WordPress already take care of that?
3
votes
1answer
5k views

Get term SLUG by term ID

I have a hierarchical taxonomy called 'tarefa' I have to display all posts from a child taxonomy. I used get_term_children function function to verify if the taxonomy parent has children. If the ...
3
votes
1answer
201 views

Get all authors with at least one post of 'custom post type'

How can i achive that? I found count_user_posts and get_users, but how would I merge them into the desired result? Do I really have to use a WP DB Query?
3
votes
2answers
238 views

Very slow query

This query works, but it takes upwards of 20 seconds to run on a Quad Core dedicated server... How can I optimize this? $slides=get_posts( array('post_type' => 'any', 'numberposts' => 5, ...
3
votes
1answer
1k views

latest 5 posts using switch_to_blog loop

im sure i must be doing something wrong and would really appreciate a little help. I am trying to loop through all of my Sub Dir "Multisite" blogs using switch_to_blog, below: global $wpdb, ...
3
votes
1answer
115 views

How to add paging to query

In the script below, I'm calling out from archive.php to functions.php to retrieve a list of posts that have been flagged in a category reserved for my "blog" posts. I'm listing these posts in summary ...
3
votes
3answers
527 views

List most recent image uploads, but only for specific custom post type

I can get a list of the most recent image attachments like so: $attachments = get_posts( array( 'post_type' => 'attachment', 'posts_per_page' => $number, 'post_mime_type' => ...
2
votes
1answer
1k views

Is it necessary to reset the query after using get_posts()?

Is it necessary to reset the query after using get_posts() ? I have been looking at this page and I don't see any reference to get_posts() ... I can't make it out for sure from this page either. I ...
2
votes
1answer
6k views

get all posts ID from a category

Problem: I need to get an array of ID's of posts from a given category If the category have any posts. This is to be used on a plugin options page. So far I have: $posts = ...
2
votes
1answer
2k views

Whats the way to format a date after using get_posts()

I see that theres no way to get a formatted date from a function like echo get_the_date($post->ID, 'd M Y'); get_the_date only works with the current $post. If I used get_posts() how can I ...
2
votes
1answer
249 views

get_posts inside cron

Edited to rewrite my question I'll try to be as clear as possible, as I need to get this resolved quickly. I disabled WordPress' cron, and added a real cron job on my server to call wp-cron.php ...
2
votes
3answers
994 views

Post count per (day/month/year) since blog began

I'm looking to echo the number of posts per month since the blog began, and for months where there was no posts echo '0'. This is the output I want: January 1, February 3, March 8, April 3, ... ...
2
votes
1answer
1k views

With get_posts(), how can I put a category as a variable

I'm a noob at this so please bear with me. I have the following code to return post title as links within categoryID 6, which is working fine: <?php global $post; $cat_posts = ...
2
votes
2answers
1k views

How to get all children and grandchildren of a hierarchical custom post type?

I need to get all sub-posts of a specific (root) parent id. get_posts( array( 'numberposts' => -1, 'post_status' => 'publish', 'post_type' => 'microsite', 'post_parent' => ...
2
votes
2answers
182 views

How to get 4 Posts after the 5 most recent ones

Well, I think the question might be a bit confusing. But well, I want to get the recent 4 posts, ignoring the first five recent posts. So in short the sixth, seventh, eighth and ninth - most recent ...
2
votes
1answer
2k views

meta_query: using BETWEEN with floats and/or casting to DECIMAL

Each post has a lat/lng value attached to it via postmeta. I'm trying to grab all posts within a bounding lat/lng value. Here's the get_posts query: $posts = get_posts(array( 'posts_per_page' ...
2
votes
3answers
3k views

More posts from the current author

On a multi author WordPress blog I need to show a list with more posts from the current author. The list is inside the Loop, so I can use <?php the_author_meta('first_name'); ?> <?php ...
2
votes
1answer
5k views

get_posts - get all posts by author id

I want to get all posts by certain author id (current user). Later, I want to pick the first post made by this user (ASC). I guess I do not use the right arguments in get_posts, am I? ...
2
votes
2answers
1k views

Why Does get_posts() Return an Empty Set?

I'm writing a custom plugin that is initialized at init. This plugin is trying to query for some custom post types already stored in the DB. Here's my code: $args = array() $myposts = get_posts( ...
2
votes
1answer
595 views

Get attachments (get_posts) and WP 3.5 new uploader

I'm using get_posts() to get attachments from the post and use it in the slider. The issue is new uploader doesn't have that "Gallery" tab which was perfectly suitable for this. It also seems to be ...
2
votes
2answers
497 views

How to generate a list of posts published on current day?

I want to create a page labeled "Today's News" which displays a simple bulleted list of all posts published on the current day and grab the current date based on the server's date/time. Is this ...
2
votes
2answers
855 views

Why can I not use setup_postdata($post) in the sidebar?

I have created a function that takes one parameter - post type, and will output each posts with some html and title, content etc within that. However, I want to be able to use functions associated ...
2
votes
2answers
507 views

get selected post using $_GET

im trying to get a post info when I click a button on the post and make it echo out the the same info it has in the else function, I tried adding in a query but in return it makes it echo back all the ...
2
votes
3answers
441 views

Trouble using get_post

For Microkids related post plugin He said "Using the get_post() function you could get any data you need from the related posts." and he posted this snippet of code. $related_posts = ...
1
vote
2answers
1k views

Using query_posts inside single.php loop

Inside of my loop in single.php, I used a custom query using get_posts to return posts belonging to a certain category. <?php global $post; ...
1
vote
2answers
213 views

Ordering posts by metadata

In my plugin, I use these instructions to obtain a list of posts: $args = array( 'numberposts' => -1, 'offset' => 0, 'meta_query' => array( array( ...
1
vote
1answer
2k views

Different Results with query('s=computer') vs get_posts('s=computer')?

I have two different pieces of code and it's returning two different results. $search_query = new WP_Query(); $search_posts = $search_query->query('s=computer'); echo count($search_posts); vs. ...
1
vote
1answer
915 views

Adding Pagination on a Custom Author Page

i have created a custom author's page that list the title of their posts. but the problem is i can't get the pagination to work if it's beyond the set post per page value. i have used get_posts() to ...