6

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

2
  • 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, 2013 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, 2017 at 7:02

2 Answers 2

4

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

a:1:{i:0;s:5:"13112";}

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

a:1:{i:0;i:13112;}

So when you try the meta query:

 'meta_query' => array(
            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:

add_filter('posts_where','my_posts_where');
$getPosts = new WP_Query($args);
remove_filter('posts_where','my_posts_where');

where

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

but I don't reccomend it.

7
  • 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, 2013 at 11:18
  • yes, that's strange, when you search in PHPMyAdmin, do you get similiar results?
    – birgire
    Jun 4, 2013 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, 2013 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, 2013 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, 2013 at 14:51
3

Fixed by using

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

No idea why...

Your Answer

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

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