Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

wp_query + redux framework & multi-select categories WordPress

  • SOLVED

Hi,

This question relates, as suggested in the title, to Redux Framework http://reduxframework.com/, and utilizing the multi-select http://docs.reduxframework.com/redux-framework/fields/select/ field to show multiple categories. For a single select, it is pretty straightforward:


<?php
$the_query = new WP_Query(array(
array( 'category' => $my_options['featured1'] ),
'posts_per_page' => 1
));
while ( $the_query->have_posts() ) :
?>

...

<?php
endwhile;
wp_reset_postdata();
?>


Where 'featured1' is the id of the select options array, which looks like this:


array(
'id' => 'featured1',
'type' => 'select',
'data' => 'categories',
'multi' => false,
'title' => __('Featured - First Section', 'siiimple'),
'subtitle' => __('', 'siiimple'),
'desc' => __('', 'siiimple'),
),


'multi' is set to false.

But if I wanted to set 'multi' to true, and allow the user to select multiple categories to show posts from a variety of categories, I don't know how to call that in the query. For example, this doesn't work, even if 'multi' is set to true:


<?php
$the_query = new WP_Query(array(
array( 'category__and' => $siiimple_options['featured1'] ),
'posts_per_page' => 1
));
while ( $the_query->have_posts() ) :
$the_query->the_post();

?>


I believe it requires using a for each loop, but not sure how to do that.

Thanks!
Justin

Answers (2)

2014-04-03

Arnav Joy answers:

can you show me the result of this

<?php

print_r($siiimple_options['featured1']);

?>


Arnav Joy comments:

please note category__and will take "," separated values if there are more than one value like

array( 'category__and' => array(1,3) )


Justin Young comments:

Yes, it outputs an array: Array ( [0] => 32 [1] => 8 )

There are currently two categories selected in the admin area, and here's what the query looks like:


$the_query = new WP_Query(array(
array( 'cat' => $siiimple_options['featured1'] )
));


Arnav Joy comments:

try this


<?php

$featured1 = implode(',',$siiimple_options['featured1'] ) ;
$the_query = new WP_Query(array(

array( 'cat' => array( $featured1 ) )

));



?>


Justin Young comments:

This didn't seem to work. I will print the whole code here, because perhaps it has something to do with that I'm using it in the bootstrap carousel, which I have setup like this:


<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<?php

$featured1 = implode(',',$siiimple_options['featured1'] ) ;
$the_query = new WP_Query(array(
array( 'cat' => array( $featured1 ) ),
'posts_per_page' => 1

));

while ( $the_query->have_posts() ) :
$the_query->the_post();
$thumbblog = get_post_thumbnail_id();
$img_url_blog = wp_get_attachment_url( $thumbblog,'siiimple-feature' );
$imageblog = aq_resize( $img_url_blog, 570,480, true );
?>
<div class="item active">
<img src="<?php echo $imageblog ?>" class="img-responsive" alt="image"/>
<div class="carousel-caption">
<h4><a href="<?php the_permalink(); ?>"><?php print_r($siiimple_options['featured1']); ?></a></h4>
<p><?php the_excerpt();?></p>
</div>
</div><!-- item active -->
<?php
endwhile;
wp_reset_postdata();
?>
<?php
$featured1 = implode(',',$siiimple_options['featured1'] ) ;
$the_query = new WP_Query(array(
array( 'cat' => array( $featured1 ) ),
'posts_per_page' => 0,
'offset' => 1
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
$thumbblog = get_post_thumbnail_id();
$img_url_blog = wp_get_attachment_url( $thumbblog,'siiimple-feature' );
$imageblog = aq_resize( $img_url_blog, 570,480, true );
?>
<div class="item">
<img src="<?php echo $imageblog ?>" class="img-responsive" alt="image"/>
<div class="carousel-caption">
<h4><a href="<?php the_permalink(); ?>">Hello</a></h4>
<p><?php the_excerpt();?></p>
</div>
</div><!-- item -->
<?php
endwhile;
wp_reset_postdata();
?>
</div><!-- carousel-inner -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev"></a>
<a class="right carousel-control" href="#myCarousel" data-slide="next"></a>
</div><!-- #myCarousel -->


There are two categories selected, and the output of it is:


Array ( [0] => 16 [1] => 34 )


Arnav Joy comments:

try passing manually once and see if that works

array( 'cat' => array( 1,2 ) ),

change 1 and 2


Justin Young comments:

This gives a syntax error. Parse error: syntax error, unexpected ';'

2014-04-04

Hariprasad Vijayan answers:

Did you try this

$the_query = new WP_Query(array(
array( 'category__and' => array(implode(",", $siiimple_options['featured1'])),
'posts_per_page' => 1
));


Justin Young comments:

This worked, Hariprasad. Except there is a ")" missing at the end of the array :)

Thanks for all the help.


Hariprasad Vijayan comments:

Glad to hear. Sorry, I didn't test the code. Just suggested it. :-)


Justin Young comments:

Unfortunately, on testing it, it didn't work. I thought it did but I must have made a mistake. Not sure why.