• I’m using category__in to include multiple category IDs but when I try to put the comma-separated list into an array it doesn’t work. This is what I have tried:

    $included_categories = array(2,21,27,29);
    $args['category__in'] = $included_categories;

    Which results in this query:

    array ( 'category__in' => array ( 0 => 2, 1 => 21, 2 => 27, 3 => 29, ) )

    But as per the WordPress Codex the correct syntax should be:

    array ( 'category__in' => array( 2,21,27,29 ) )

    I can’t figure out how to create an array of purely comma-separated IDs without having the key/value pairs. Note: I can’t just write out the query as instructed by the Codex because I’m building it out as shown in my code above.

Viewing 1 replies (of 1 total)
  • Actually, you get the same results either way:

    $args = array(
       'posts_per_page' => 6,
       'category__in' => array(1,2,3,4)
    );
    print_r('<pre>ARGS:');print_r($args);print_r('</pre>');
    
    $cat_array = array(1,2,3,4);
    $args['category__in'] = $cat_array;
    print_r('<pre>ARGS:');print_r($args);print_r('</pre>');
Viewing 1 replies (of 1 total)
  • The topic ‘How do I include multiple category IDs in WordPress query?’ is closed to new replies.