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?
post_author
is not a valid parameter forget_posts
. You really need to look atWP_Query
‘s argument list to see that, asget_posts()
is really just a wrapper around that class.What you want is
author
without thepost_
part. Try that, and it should work.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 ofWP_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 callwp_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.