Ask your WordPress questions! Pay money and get answers fast! (more info)

Category_Name with Get_Option WordPress

  • SOLVED

I am trying to pass the values of a multiselect category option through the loop to display posts based on the selected categories. With the code I have below, only the last value in the array is being used to display the posts. For example, if my selected categories are Summer, Fall & Spring - only the posts categorized as "Spring" are displayed. Additionally, if I do echo $term->name, all selected categories are listed.

How do I ensure all selected categories are used as parameters for the loop?


$types = get_option (jqs_select_categories);
foreach ($types as $type) {
$term = get_term_by ('name',$type,'category');
}

$wpbp = new WP_Query(array( 'post_type' => 'post', 'category_name' => $term->name, 'posts_per_page' => '100', 'paged' => $paged) );

Answers (1)

2014-05-26

Hariprasad Vijayan answers:

Hello,

Try this code


$types = get_option (jqs_select_categories);
$cats = array();
foreach ($types as $type) {
$term = get_term_by ('name',$type,'category');
$cats[] = $term->term_id;
}
$wpbp = new WP_Query(array( 'post_type' => 'post', 'category__in' => $cats, 'posts_per_page' => '100', 'paged' => $paged) );

Let me know if you have trouble.

Regards,
Hariprasad


Louise comments:

Perfect, thanks!