0

I am trying to dynamically create a meta query based upon an array of post_id values. However, the query is not working. I have used var_dump() to see $meta_array, and it appears to be a standard array in the correct format to pass into WP_Query().

$post_id_array = array( "12", "24" ); //this array will be dynamically generated
$meta_array = array();
foreach ($post_ids_array as $key => $value) {
    array_push($meta_array,
        array(
            'key' => 'relate_blog_posts',
            'value' => $value,
            'compare' => 'LIKE'
        )
    );
}

$post_args = array(
    'post_type' => 'post',
    'posts_per_page' => 3,
    'post_status' => 'publish',
    'meta_query' => array($meta_array)
 );
$post_query = new WP_Query($post_args);

Please let me know if I have made a mistake, or am simply going about this the wrong way.

Thanks!

  • Does it work if you just manually create the array? You should check that to see if the issue is the actual query or the variable. My first reaction is that you seem to be storing all related posts in a single meta value, possibly as a serialised array. This makes it very difficult to query. – Jacob Peattie May 26 '18 at 2:38
0

First of all, you have a typo in your code. The variable with post IDs is called post_id_array, but your foreach loop uses post_ids_array (ids instead of id).

When you fix this, then there's one more problem. Meta query param should be an array of queries and each query should be an array. But your $post_args generated by your code looks like this:

array(4) {
  ["post_type"]=>
  string(4) "post"
  ["posts_per_page"]=>
  int(3)
  ["post_status"]=>
  string(7) "publish"
  ["meta_query"]=>
  array(1) {
    [0]=>
    array(2) {
      [0]=>
      array(3) {
        ["key"]=>
        string(17) "relate_blog_posts"
        ["value"]=>
        string(2) "12"
        ["compare"]=>
        string(4) "LIKE"
      }
      [1]=>
      array(3) {
        ["key"]=>
        string(17) "relate_blog_posts"
        ["value"]=>
        string(2) "24"
        ["compare"]=>
        string(4) "LIKE"
      }
    }
  }
}

As you can see, meta_query is an array containing an array containing queries.

And here is your code with all the fixes:

$post_ids_array = array( "12", "24" ); //this array will be dynamically generated
$meta_array = array();
foreach ($post_ids_array as $key => $value) {
    array_push($meta_array,
        array(
            'key' => 'relate_blog_posts',
            'value' => $value,
            'compare' => 'LIKE'
        )
    );
}

$post_args = array(
    'post_type' => 'post',
    'posts_per_page' => 3,
    'post_status' => 'publish',
    'meta_query' => $meta_array
);
$post_query = new WP_Query($post_args);
  • Thanks so much for responding @Krzysiek!!! Although your answer was not the complete solution, it was extremely helpful in leading me to the answer. The missing piece was the 'relation' key in the meta array! See the complete answer below – CandyPaintedRIMS May 31 '18 at 16:31
  • @CandyPaintedRIMS well, it’s impossible to guess things that you don’t put into question. I’ve fixed the things that were clearly wrong in your code form WP perspective. – Krzysiek Dróżdż May 31 '18 at 16:33
0

With the help of @Krzysiek Dróżdż I have found the solution to this issue. My first issue was that I was nesting the $meta_array in another array.

As suggested by @Krzysiek, I changed this line

'meta_query' => array($meta_array)

to

'meta_query' => $meta_array

. This allowed for the meta array to remain in the correct syntax for the WP_Query object. The final issue that I resolved was that in a meta query with multiple values, you must define how the values are related. For instance

'meta_query' => array(
        array (
           'key' => 'relate_blog_posts',
           'value' => '155',
           'compare' => 'LIKE'
        ),
        array (
           'key' => 'relate_blog_posts',
           'value' => '156',
           'compare' => 'LIKE'
        )
    )

will not return any values, while the following will:

'meta_query' => array(
        'relation' => 'OR',
        array (
           'key' => 'relate_blog_posts',
           'value' => '155',
           'compare' => 'LIKE'
        ),
        array (
           'key' => 'relate_blog_posts',
           'value' => '156',
           'compare' => 'LIKE'
        ),
    )

Thus by initially defining $meta_array to include the 'relation' attribute, the final array was in the proper syntax and the query worked. Final working code:

$post_ids_array = array( "12", "24" ); //this array will be dynamically generated
$meta_array = array('relation'=>'OR');
foreach ($post_ids_array as $key => $value) {
    array_push($meta_array,
        array(
            'key' => 'relate_blog_posts',
            'value' => $value,
            'compare' => 'LIKE'
        )
    );
}

$post_args = array(
    'post_type' => 'post',
    'posts_per_page' => 3,
    'post_status' => 'publish',
    'meta_query' => $meta_array
);
$post_query = new WP_Query($post_args);

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.