0

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.

4
  • Stupid question, but did you debug the (second part of the) meta query? Is $catconsole set? Is it what your posts have as meta value? BTW, you really should consider moving from metas to a custom taxonomy, which you add for pages as well as posts.
    – tfrommen
    Commented Dec 11, 2014 at 21:56
  • Seems that the query is not getting the review category. $catconsole and $newtitle are fine, I even hardcoded data and still showing results from all platforms/categories... Commented Dec 11, 2014 at 23:14
  • You're right! The vars where all right, but in fact there is another review with the same title forgotten in a bunch of posts, and just on that one, the custom field "platform" was set to SNES as well, so I really have two reviews... So all the args are OK, I've added only one more ('ignore_sticky_posts' => 1). Stupid mistake! Sorry people... Commented Dec 12, 2014 at 0:36
  • @DanielLemes Would you like to post this solution as an answer? You're welcome to do so and then accept the answer, that way this question won't show up as unanswered :)
    – Tim Malone
    Commented Jun 29, 2016 at 5:52

2 Answers 2

0

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.

1
  • No, it's a select. Commented Dec 11, 2014 at 23:12
0

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

It IS working. The code is OK, I have just make a minor mistake: there was two reviews under "snes" category, I forgot to change one of them when testing. I've added only one more ('ignore_sticky_posts' => 1). Stupid mistake, sorry about it.

Your Answer

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

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