1

According to the codex, to query posts that can belong to any of a collection of categories, we need to do something like this:

$query = new WP_Query( array( 'cat' => '2,6,17,38' ) );

https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

However, I have the following, which works, and I'm not sure why.

  $args = array(
    'cat' => $related_cat_ids,
    'posts_per_page' => -1,
  );
  $query = new WP_Query($args);

The reason why I'm confused is because $related_cat_ids is actually an array, but the example clearly shows a string of numbers, seperated by commas, for the 'cat' value.

Is this something I should be concerned about? Will my code all of a sudden stop working in a future WordPress release? It's more convenient for my case to keep using the array, rather than format it into that string. The reason being is that I'm using get_the_category() to get the categories dynamically, and then pushing the IDs from that array into my new array.

2

I understand your confusion but you haven't something to worry about as behinds the scenes Wordpress make all the necessary steps to make it work.

For example, take the following code from the class-wp-query.php

// If querystring 'cat' is an array, implode it.
if ( is_array( $q['cat'] ) ) {
    $q['cat'] = implode( ',', $q['cat'] );
}

Is checking to see if is an array and quickly transform the result to a comma-separated string.

Another way will be using the other options like:

 category__and           An array of category IDs (AND in).
 category__in            An array of category IDs (OR in, no children).
 category__not_in        An array of category IDs (NOT in).

More on this in Category_Parameters

But again I would hold to use the way you find more comfortable.

It perfectly fine to use the array() format if you find it more useful besides this is what make Wordpress a fun and useful tool to work on.

  • 2
    bonus votes if you edit your Q to mention category__in – Tom J Nowell Nov 16 '17 at 18:17
  • I originally tried "category__in" but it didn't work out. Got error "Notice: Trying to get property of non-object in /wp-includes/class-wp-query.php on line 3494". Anyway, thank you for the code reference. It really is handy to pass in the array, so as long as they keep that code, it's all good. – Jordan Carter Nov 16 '17 at 19:25
0

Yes, it will work. The info may not be provided on the codex. But if you look at the code here regarding the parameter, you will find that even if an array is passed, it is changed to string.

    // If querystring 'cat' is an array, implode it.
    if ( is_array( $q['cat'] ) ) {
        $q['cat'] = implode( ',', $q['cat'] );
    }

So you should be safe. So far as I am concerned, it is a good thing that both string and array is accepted as arguments and WordPress community will prolly let it be as it is.

Even if you still have doubt you can always use category__in parameter which always takes an array as below:

$args = array(
'category__in' => $related_cat_ids,
'posts_per_page' => -1,
);
$query = new WP_Query($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.