I have a custom post type "custom_author" which is selected when making a new post using a metabox dropdown with field key "chosen_author".
This is what my wp_postmeta looks like:
Where 13088 is the blog post that has "author" 13112 assigned to it.
I am trying to get all posts by a particular author using the following:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'chosen_author',
'value' => '13112' (tried with and without quotes)
)
)
);
$getPosts = new WP_Query($args);
But it just returns ALL my posts.
I did some debugging:
$getPosts = new WP_Query($args);
echo $GLOBALS['wp_query']->request;
and that output
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND (wp_posts.post_author != 0) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 10
Completely ignoring the meta_query array!
If it matters this being used outside the loop
$GLOBALS['wp_query']->request
isn't going to be your query since your query isn't$wp_query
, global or otherwise, it is$getPosts
.echo $getPosts->request
is what you want.$getPosts
why not just$getPosts->posts
?->request
contains REQUEST aka the SQL query for your posts, not the RESULTS ... :)