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:

enter image description here

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(
                '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

| improve this question | | | | |
  • 1
    $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. – s_ha_dum Jun 4 '13 at 15:07
  • If you store $getPosts why not just $getPosts->posts ? ->request contains REQUEST aka the SQL query for your posts, not the RESULTS ... :) – jave.web Jun 12 '17 at 7:02

The problem

It looks like you are saving the data as:

$a = array( "13112" );
update_post_meta( $post_id, "article_author", $a);

where this will give you the meta_value


Your first query will give you escaped quotes:

LIKE '%\"13112\"%'

Method 1

If you save the author array with numbers instead:

$a = array( 13112 );
update_post_meta( $post_id, "article_author", $a);

then the corresponding meta_value will be


So when you try the meta query:

 'meta_query' => array(
                'key' => 'article_author',
                'value' => ':13112;',
                'compare' => 'LIKE'

the corresponding SQL part will be

LIKE '%:13112;%'

You could try to use this instead.

Method 2

Another way is to remove the escaped quotes with non-escaped quotes:

$getPosts = new WP_Query($args);


function my_posts_where($where){    
    $where = str_replace('\"', '"', $where);    
    return $where;  

but I don't reccomend it.

| improve this answer | | | | |
  • Thank you for this, I don't have much control over the format saving (string/integer), out of interest, why does it return all posts if it clearly can't find a match? How am I supposed to handle no matching posts found if it returns all my posts? Edit: OK it doesn't return ALL my posts but it does return 100s – Titan Jun 4 '13 at 11:18
  • yes, that's strange, when you search in PHPMyAdmin, do you get similiar results? – birgire Jun 4 '13 at 11:48
  • I was using a plugin to help me with custom meta boxes (a drop down of my custom post type entries). I've dived in and did it all myself in functions.php and now I save in the db just the number as the meta_value: 13112 Still no joy though, I'll see if I can turn on some sort of debugging that outputs the SQL query it is attempting to perform. – Titan Jun 4 '13 at 14:09
  • Weird, I used $GLOBALS['wp_query']->request; to find out and it outputted 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!! – Titan Jun 4 '13 at 14:15
  • 1
    ok good to hear you found a way around your problem ;-) You should check your SQL with $getPosts->request instead. – birgire Jun 4 '13 at 14:51

Fixed by using

get_posts($args) instead of new WP_Query($args);

No idea why...

| improve this answer | | | | |

I too faced a similar problem but couldn't get the query to work correctly.

To write the data, I used the following codes:

$post_id = 2;
$user_key = 'ldp_system_follows_user_1';
$user_data_new = array(
    'followed'    => 'nofollowed'

update_post_meta($post_id, $user_key, $user_data_new);

The above code persisted the following in db when executing get_post_meta(post_id);

  "ldp_system_follows_user_1": [

I wanted to retrieve all the post ids that matches the meta data I wrote but it returned an empty array.

$args = array(
        'post_type'     => 'post',
        'post_status'   => 'publish',
        'meta_query'    => array(
                                    'key'   => $user_key,
                                    'value' => $user_data_new,
                                    'compare'   => 'LIKE'

    return  get_posts($args);

Any help would be greatly appreciated.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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