0

I'm trying to get posts from 2 or more categories, so far I discovered:

$args = array( 
    'category__in'=>array( implode(ot_get_option("beautiful_categories"), ",")  )
);
$query = new WP_Query( $args );

I'm using OptionTree and that will return categories ids with "," between them. and then I get post with while loop, but it just return posts of first category.

How can i fix it?

0

The 'category__in' parameter accepts an array. You're giving it a string of comma-separated values. Also, you need to use explode() instead of implode(). implode() is for joining array elements with a string, while explode() is for splitting a string by another string. Try this instead:

$args = array( 
    'category__in' => array( explode( ot_get_option( 'beautiful_categories' ), ',' ) ),
);
$query = new WP_Query( $args );
0

that was my error, OptionTree for this input type(radio in my case) itself returns an array and we don't need to convert it. and category__in needs an array so i just change it to:

$args = array( 
    'category__in' => ot_get_option( 'beautiful_categories' )
);
$query = new WP_Query( $args );

and it worked perfectly.

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.