2

I want to retrieve an array with the posts of a certain user for a custom post type category.

However, get_posts($arg) returns ALL posts of this custom post type, although I specified the post_author

<?php $pages = get_posts(array('post_type'  => 'profile','post_author' => $post->post_author)); ?>

<?php var_dump ($pages); ?>

In this example $post->post_author is 11. However, the result of the code above is:

array(3) { 
[0]=> object(WP_Post)#343 (24) { 
["ID"]=> int(2326)
["post_author"]=> string(2) "11" 
..etc.} 
[1]=> object(WP_Post)#352 (24) { 
["ID"]=> int(2324) 
["post_author"]=> string(1) "0" 
 ...etc.}
[2]=> object(WP_Post)#395 (24) { 
["ID"]=> int(2322)
["post_author"]=> string(1) "0" 
...etc.} 

Why does get_posts() returns posts of authors whose ID is not 11?

2 Answers 2

6

post_author is not a valid parameter for get_posts. You really need to look at WP_Query's argument list to see that, as get_posts() is really just a wrapper around that class.

  • author (int) - use author id.
  • author_name (string) - use 'user_nicename' (NOT name).
  • author__in (array) - use author id (available with Version 3.7).
  • author__not_in (array) - use author id (available with Version 3.7).

http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters

What you want is author without the post_ part. Try that, and it should work.

1
  • The simple things are the ones that drive it ... +1
    – kaiser
    Commented Mar 16, 2014 at 0:59
0

As it is totally unclear where $post->post_author comes from, the most likely issue is, that you are either not referring to the $GLOBALS['post'] instance of WP_Post or that you have another query after your main loop, which replaces the main query.

You best shot probably will be to use wp_reset_query(). This will realign the $GLOBALS['wp_query'] object which holds the current query with $GLOBALS['wp_the_query'] again, which holds the main query and afterwards call wp_reset_postdata() to rewind the main query.

If that doesn't help, then the chance that some plugin (or you) or the theme used query_posts(), which overwrites the global $wp_the_query object and therefore messes up your loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.