0

I have this pretty standard script that looks for a preview or a report (custom post types) which have data stored against them (serialized array) taking the form of a guid.

The guid is present on my report and not present on my preview (this is intended as I want to catch the failure).

However, when I run the script for preview, the get_posts returns one post which seems to be the latest post published.

$args = array(
    "post_type" => array($type),
    "posts_per_page" => 1,
    "post_status" => "publish",
    "orderby" => "date",
    "order" => "DESC",
    "meta_query" => array(
        "key" => "_fixtures",
        "value" => $fixguid, // IN THIS CASE IT SHOULD BE 12AB1324
        "compare" => "="
    )
);  
$posts = get_posts($args);
return $posts;

The report (which has the meta data 12AB1324) is returned successfully but the preview returns the latest story which happens to have a value of 12AB3136.

I suppose my question is:

Why does get_posts meta query return
a) the latest post and
b) a post with a guid that doesn't = the given key parameter
.

Thanks.

  • Searching for posts via their meta values can be hideously expensive. I see no reason this couldn't be done with a custom taxonomy named fixtures, it'd be much, much faster – Tom J Nowell May 19 '17 at 0:50
0

The meta_query parameter always need to be an array of array and should be written like this

"meta_query" => array(
     array(
         "key" => "_fixtures",
         "value" => $fixguid, 
         "compare" => "="
     )

In fact, the array of array is use to serve to any relation when you want to query multiple metas, but it's need even you query only one.

Hope it works after !

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.