3

I have a custom post type called 'game' with some ACF fields. One of the fields is a Post Object(called review_link) that accepts posts from a category called 'reviews'. One other field is a taxonomy field(called gametags) looking for the post_tag taxonomy of the 'posts'

This is what I am trying to achieve:

  1. Whenever you are reading a post (post_type=post) within the category Reviews to be able to add information to the sidebar, from the Game (post_type=game) which has this specific post in the Post Object field.

  2. Whenever you are reading a post (post_type=post) from any category, to be able to get that posts tags, and look in the Games (post_type=game) ACF fields, and specifically in the taxonomy field, to find any matching tags, and then show some Game info.

My code for case 1 is the following and it works great:

$reviewID = $post->ID;
if(in_category('reviews') ) {

    $reviewArgs = array(
      'post_type'    => 'game',
      'meta_query' => array(
         array(
           'key' => 'review_link',
           'value' => ''.$reviewID.'',
           'compare' => 'LIKE'
         )
       )
    );

}

Now for case 2 I have tried the following:

$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );

$reviewArgs = array(
   'post_type' => 'game',
   'meta_query' => array(
         array(
             'key' => 'gametags',
             'value' => $tag_ids,
             'compare' => 'IN'
         )
    ) 
);

The $tag_ids for an example post are: 818,436,435,43,46,77. If I replace the $tag_ids in the above code with 435 it matches. But if I try to add it like an array it doesn't. I have also tried serializing the array, looping the array etc. None of it works. I could make an array of meta queries but the thing is the number of tags in each post are random. I have also tried converting the array to a string and using'LIKE' instead of 'IN' and nothing.

Actually the above code throws a warning: Warning: trim() expects parameter 1 to be string, array given in /.../public_html/wp-includes/class-wp-meta-query.php on line 594

I have tried almost everything to make case 2 work and could not. From what I realize the meta_query is not working as expected when the meta value is an array, although the Codex specifies that it can be an array. All the above code is placed in sidebar.php.

Any hints tips would be appriciated

Thanks in advance

2 Answers 2

1

I don't know if you can get one array to compare to the other directly, but you can create a loop to set up a meta_query array that will check for each of the IDs within the field separately:

$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );

$meta_query = array('relation' => 'OR');

foreach ($tags_ids as $tag_id) {
    $meta_query[] = array(
        'key' => 'gametags',
        'value' => $tag_id,
        'compare' => 'IN'
    );
}

$reviewArgs = array(
   'post_type' => 'game',
   'meta_query' => $meta_query
);

Note: this could produce errors if no tags are set for the post so you might want to add some alternative handling for that case.

EDIT: try this to test for the exact matches later...

$review_query = new WP_Query( $reviewArgs ); 
while( $review_query->have_posts() ) { 
    $review_query->the_post(); 
    global $post; $checkmatch = false;
    $gametags = get_post_meta($post->ID,'gametags');
    if (!is_array($gametags)) {$gametags = explode(',',$gametags);}
    foreach ($tags_ids as $tag_id) {
        if (in_array($tag_id,$gametags)) {$checkmatch = true;}
    }
    if ($checkmatch) {
         // echo output
    }
}
10
  • Thanks a lot! This way it works! Is there a way to be more specific in the comparison? Because IN returns '152' and '1524'?
    – Pantso
    May 19, 2016 at 11:05
  • try = or EQUALS instead if you are looking for an exact match
    – majick
    May 19, 2016 at 11:09
  • hang on no, are you sure, wouldn't LIKE do that rather than IN?
    – majick
    May 19, 2016 at 11:12
  • Yeah right now with a live example if I use IN it gets '14739' and '14885'! LIKE and = don't work! Hm maybe there is a problem in my logic? Anyway thank you for your time :)
    – Pantso
    May 19, 2016 at 11:18
  • sorry my bad IN should really be it, but are you setting gametags as an array or a string? it may make the difference because 152 is in the string 1524 but not in the array('1525') if you see what I mean.
    – majick
    May 19, 2016 at 11:19
0

Since $tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); returns an array, you should be able to use it like

$reviewArgs = array(
   'post_type' => 'game',
   'meta_query' => array(
         array(
             'key' => 'gametags',
             'value' => $tag_ids,
             'compare' => 'IN'
         )
    ) 
);

Do you have the metakeys gametags for the post-type games? Also are you using the WP_Query? Or get_posts()?

1
  • Yeah the rest of the code is $review_query = new WP_Query( $reviewArgs ); while( $review_query->have_posts() ) { $review_query->the_post();
    – Pantso
    May 19, 2016 at 11:04

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.