How to query posts by meta keys AND under specific category?

The scenary:

I’m working in a review system for WordPress, a game review website. It uses default posts, that when under “review” category, shows the review’s meta fields (from ACF plugin). It’s pretty usual, no secrets.

The difference: using a custom post type, other page will “catch” data from reviews and show the average score of that game, from X reviews.

So, if I have two reviews of “Mortal Kombat”, the general info page – a custom post “game” – will query for reviews with the slug “review-mortal-kombat-snes”, calculate the average score in each criteria (graphics, sound, etc) and show it as graphic information.

The review system is ready and working fine, but I have a problem to catch only the reviews data at the “game” page. This is the WP_Query that I trying to use to find reviews:

$args = array(
        'numberposts' => -1,
        'post_type' => 'post',
        'post_status' => 'publish',
        'category_name' => 'review',
        'meta_query' => array(
        'relation' => 'AND',
            array(
            'key' => 'acf_gametitle',
            'value' => $newtitle,
            'compare' => '='
            ),
            array(
            'key' => 'acf_platform',
            'value' => $catconsole,
            'compare' => '='
            )
        )
    );
    $the_query = new WP_Query( $args );
  1. “acf_gametitle” is a meta field with the name of the game (from reviews).

  2. $newtitle stores the name of the game (from the game page).

  3. “acf_platform”, of course, stores the platform (SNES, Sega Genesis, etc).

  4. $catconsole stores the custom category of the game page (the platform name).

What I need:

First, query for post type “post”, with the category “review”, where the custom field “acf_gametitle” have the same value of the var $newtitle from the game page.

So, if the page is about “Mortal Kombat”, it must find all reviews with the title “Mortal Kombat”.

AND second: the custom field “acf_platform” from review must have the same value of the var “$catconsole”.

So, it will not get values from reviews of Mortal Kombat to SNES and arcade – only the proper platform.

It’s not working, keeps giving me data from reviews of all consoles: Mortal Kombat from SNES and arcade are counted as “two reviews for Mortal Kombat” at the SNES page of the game:

$string_count = $the_query->found_posts; 
echo $string_count;

Is there anything wrong? Thanks in advance for any help.

Answer

Are you using a checkbox or similar for the ‘acf_platform’ field using ACF for more than one selection?

Advanced Custom Fields stores these as a serialised array, so a meta_query with ‘compare’ => ‘=’ may not work.

The ACF documentation suggests using ‘compare’ => ‘LIKE’

$args = array(
   'meta_query' => array(
         array(
            'key' => 'field_name', // name of custom field
            'value' => '"red"', // matches exaclty "red", not just red. This prevents a match for "acquired"
            'compare' => 'LIKE'
        )
   )
);

http://www.advancedcustomfields.com/resources/checkbox/


EDIT: Actually, I realised that if this was the case your query should be returning no results. You mentioned it returned both posts, so this may not be the case.

Attribution
Source : Link , Question Author : Daniel Lemes , Answer Author : Craig Bilski

Leave a Comment