• Resolved 916VT

    (@vincent916)


    Hi there,

    I need to display two posts from a query but excluding the current category.

    <?php
    
      // The Query
      $cat_ID = get_the_category($post->ID);
      $post_cat = $cat_ID[0]->cat_ID;
    
      $args = array(
    	'posts_per_page' => 2 ,
    	'orderby' => 'rand'
      );
    
      $the_query = new WP_Query( $args );
    
      // The Loop
      if ( $the_query->have_posts() ) {
    	echo '<ul>';
    	while ( $the_query->have_posts() ) {
    		$the_query->the_post();
    		if ( ! in_category( $post_cat ) ) {
    		  echo get_the_excerpt();
    		  } else {
                        //HERE IS THE ISSUE
                      }
    		}
    	echo '</ul>';
      } else {
      // no posts found
      }
      wp_reset_postdata();
    ?>

    Right now, it stops when it gets to a post within that current category becausse it just gets blank.

    So can I have the two posts to display ?

    Thanks a lot for any help πŸ™‚

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter 916VT

    (@vincent916)

    I tried to use that to exclude the category from the source :

    $args = array(
    	'cat' => -$post_cat ,
    	'posts_per_page' => 2 ,
    	'orderby' => 'rand'
    );

    Unfortunately, it breaks somehow the split of languages based on Polylang and I get a mix of content.

    That’s why I’m tried the ! in_category way upper.

    @916VT

    The problem with your first code is that you only fetch two posts. Assuming that at least one of them is in current category there’s no way to display two posts… If both are from current category then by using if… in_category() you simply exclude all fetched posts!

    In theory your second code is much better and I’d say it’s at least compliant with WordPress Codex. I’m not sure what mess can Polylang cause but I’d definitely go this way…

    Good luck and if I’ll manage to find something about problems with Polylang and custom queries I’ll let you know.

    A.

    Thread Starter 916VT

    (@vincent916)

    Thanks Adam for your feedback !

    Yeah, but all my tries with the second code (‘cat’ => -$post_cat) killed Polylang someway… I’m pushing the first option on my side.

    Thanks again for helping πŸ™‚

    @916VT

    It’s just a “semi-blind shot” (I don’t have Polylang installed anywhere at the moment) but perhaps you try something like this?

    $mylang = get_query_var( 'lang' );
    $args = array(
    ‘cat’ => -$post_cat ,
    ‘posts_per_page’ => 2 ,
    ‘lang’ => $mylang,
    ‘orderby’ => ‘rand’
    );`
    `

    Thread Starter 916VT

    (@vincent916)

    Hum nope, it did not work… =(

    I made some tries to get the current langage from another way, but same result.

    All right, I guess I’ll install polylang on my test WP and give it a try πŸ™‚

    Thread Starter 916VT

    (@vincent916)

    Very nice from you πŸ™‚

    For information, I also tried to add manually the category IDs and it still breaks Polylang posts split

    $args = array(
    		'cat' => -78,-79 ,
    		'posts_per_page' => 2 ,
    		'orderby' => 'rand'
    	);

    Thread Starter 916VT

    (@vincent916)

    I keep looking from your $mylang idea, and I got that :

    $language = get_locale(); if ($language == 'fr_FR') { $mylang =  fr ; } else { $mylang = en ;}
    
        $args = array(
            'cat' => -78,-79 ,
            'lang' => $mylang,
            'posts_per_page' => 2 ,
            'orderby' => 'rand'
        );
        $the_query = new WP_Query( $args );

    It avoids the mix of languages, which is cool.

    But for some reasons, it still doesn’t care about the ‘cat’ => -78,-79 , even here when I write directly the categories ids…

    Thread Starter 916VT

    (@vincent916)

    'cat' => '-78,-79' ,

    Now it seems to work just fine πŸ™‚

    Good to know! Always happy to help πŸ™‚

    A.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Display 2 posts from wp_query with an exclude category?’ is closed to new replies.