• Resolved greencode

    (@greencode)


    I’m using the following code but only the first category that I add in the category__and section is showing in the front end. I need both to show.

    Not sure what I’m doing wrong.

    <?php
    $my_query = new WP_Query(array(
    	'category__and'	=> array('471,282')
    ));
    var_dump( $cats );
    while ( $my_query->have_posts() ) : $my_query->the_post();
    ?>
    <?php the_title() ?>
    <?php endwhile;
    wp_reset_query(); ?>

    Any ideas or help would be really appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • what are all the categories of the post(s) that gets shown?

    are you aware of the differences between 'category__and' and 'category__in' ?

    https://codex.www.ads-software.com/Class_Reference/WP_Query#Category_Parameters

    Thread Starter greencode

    (@greencode)

    Hi. Thanks for responding.

    With regards to ‘category__and’ and ‘category__in’ – Yep, I understand that category__in will show posts that are either in ID 471 or 282 whereas category__and will show posts in 471 and 282.

    What happens is if I have it this way around 471,282 then posts from cat ID 471 display. If I have it 282,471 then only posts from cat 282 display.

    Very strange.

    just saw that you are using a string within the array;

    change it to numbers:

    'category__and'	=> array(471,282)
    Thread Starter greencode

    (@greencode)

    If I add it like that then nothing displays.

    yes, it will only show posts that have both categories at the same time.

    to show posts from either the one or the other category, or both, you need to use:

    'category__in'	=> array(471,282)
    Thread Starter greencode

    (@greencode)

    Really appreciate your help with this and for helping me to finally understand the in or and! Keep on getting confused with that one, for some reason.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘category__and only showing one category's posts’ is closed to new replies.