• wpgit

    (@wordpressgit)


    i have a homepage with multiple WP_query loops and i want to prevent the featured post from showing up in its specific category loop at the same time. i only want it to show up in the category loop when it is no longer the featured post.

    here is my featured loop:

    <?php
    $featuredPost = new WP_Query();
    $featuredPost->query('showposts=1&category_name=featured');
    while ($featuredPost->have_posts()) : $featuredPost->the_post(); 
    
    stuff
    
    <?php endwhile; ?>
    here are my category loops:
    
    <?php
    $categoryPost = new WP_Query();
    $categoryPost->query('showposts=3&category_name=news');
    while ($categoryPost->have_posts()) : $categoryPost->the_post(); 
    
    stuff
    
    <?php endwhile; ?>
    
    <br/>
    
    <?php
    $categoryPost = new WP_Query();
    $categoryPost->query('showposts=3&category_name=sport');
    while ($categoryPost->have_posts()) : $categoryPost->the_post(); 
    
    stuff
    
    <?php endwhile; ?>
    
    <br/>
    
    <?php
    $categoryPost = new WP_Query();
    $categoryPost->query('showposts=3&category_name=art');
    while ($categoryPost->have_posts()) : $categoryPost->the_post(); 
    
    stuff
    
    <?php endwhile; ?>

    any help would be much appreciated.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter wpgit

    (@wordpressgit)

    thanks for the link. i don’t think this will work though as its only for the built in posts types. i’m using custom WP_query loops.

    get the ID of the featured post from the first query and exclude it from the category loops.

    i.e. in the first loop after setting up post data

    $featured_posts[] = get_the_ID();

    then in the cat loops querys add
    ‘exclude=$featured_posts’

    Something like that.

    Also have a read of this – https://codex.www.ads-software.com/Class_Reference/WP_Query

    You can send query params as args in the class init.

    Thread Starter wpgit

    (@wordpressgit)

    i’ve tried it with this code and no luck. have i set it up wrong do you know?

    many thanks.

    featured loop:
    
    <?php
    $featuredPost = new WP_Query();
    $featuredPost->query('showposts=1&category_name=featured');
    while ($featuredPost->have_posts()) : $featuredPost->the_post();
    $featured_posts[] = get_the_ID(); ?>
    
    stuff
    
    <?php endwhile; ?>
    
    category loops:
    
    <?php
    $categoryPost = new WP_Query();
    $categoryPost->query('showposts=3&category_name=news&exclude=$featured_posts');
    while ($categoryPost->have_posts()) : $categoryPost->the_post(); ?>
    
    stuff
    
    <?php endwhile; ?>
    
    <br/>
    
    <?php
    $categoryPost = new WP_Query();
    $categoryPost->query('showposts=3&category_name=sport&exclude=$featured_posts');
    while ($categoryPost->have_posts()) : $categoryPost->the_post();  ?>
    
    stuff
    
    <?php endwhile; ?>

    i’m using custom WP_query loops.

    That makes absolutely zero difference. If you had read that page, you would have seen the “Don’t Show Sticky Posts” sub0section which deals with exactly this kind of situation.

    Thread Starter wpgit

    (@wordpressgit)

    esmi –

    apologies. my wordpress is pretty rubbish i don’t really know what i’m doing.

    i don’t really want to assign my featured post as sticky. i just want to assign category ‘featured’ along with its other category which will be something like ‘news’ or ‘sport’ or ‘music’ etc.

    Look at the example I suggested and use 'post__not_in' => get_option( 'sticky_posts' ) as one of your query parameters.

    You can actually do it by doing this

    $query = new WP_Query(‘cat=-12’);

    12 being the id of you featured category (could be any number)

    have a read through the link I gave you and set up the queries as shown in that page.

    Thread Starter wpgit

    (@wordpressgit)

    hi lee,

    i don’t want to exclude posts assigned ‘featured’ from the other loops completely, but only when they are being displayed in the ‘featured’ loop itself.

    so if a post is assigned ‘featured’ and ‘news’ at the same time, i dont want it being displayed in ‘news’ while its in ‘featured’ loop.

    so what I said first is probably the way forward – it should just work, re structure your ‘new WP_Query’ calls the way they have them in the docs.

    Might be down to something like the fact you’re not calling wp_reset_postdata() after each loop?

    Try and re jig it to conform with the docs and let me know if it works or not.

    Thread Starter wpgit

    (@wordpressgit)

    hi lee,

    i used your first method and conformed exactly to the the wordpress docs found on this page

    its still not working however. is it something to do with the fact that the second loop in the docs is not new WP_query?

    many thanks.

    heres my new code:

    featured loop:
    
    <?php
    $featuredPost = new WP_Query();
    $featuredPost->query('showposts=1&category_name=featured');
    while ($featuredPost->have_posts()) : $featuredPost->the_post();
    $do_not_duplicate = $post->ID; ?>
    
    stuff
    
    <?php endwhile; ?>
    
    category loops:
    
    <?php
    $categoryPost = new WP_Query();
    $categoryPost->query('showposts=3&category_name=news');
    while ($categoryPost->have_posts()) : $categoryPost->the_post();
    if( $post->ID == $do_not_duplicate ) continue; ?>
    
    stuff
    
    <?php endwhile; ?>
    
    <br/>
    
    <?php
    $categoryPost = new WP_Query();
    $categoryPost->query('showposts=3&category_name=sport');
    while ($categoryPost->have_posts()) : $categoryPost->the_post();
    if( $post->ID == $do_not_duplicate ) continue; ?>
    
    stuff
    
    <?php endwhile; ?>
    $posts_to_exclude = array();
    			$args = array('showposts' => 1, 'category_name' => 'featured');
    			$query = new WP_Query( $args );
    
    			// The Loop
    			if ( $query->have_posts() )
    			{
    				while ( $query->have_posts() )
    				{
    					$query->the_post();
    					$posts_to_exclude[] = $query->post->ID;
    					echo get_the_title();
    				}
    			}
    
    			/* Restore original Post Data */
    			wp_reset_postdata();
    
    			$args2 = array('showposts' => 3, 'category_name' => 'news', 'post__not_in' => $posts_to_exclude);
    			$query2 = new WP_Query( $args2 );
    
    			// The Loop
    			if ( $query2->have_posts() )
    			{
    				while ( $query2->have_posts() )
    				{
    					$query2->the_post();
    					echo get_the_title();
    				}
    			}
    
    			/* Restore original Post Data */
    			wp_reset_postdata();
    
    			$args3 = array('showposts' => 3, 'category_name' => 'sport', 'post__not_in' => $posts_to_exclude);
    			$query3 = new WP_Query( $args3 );
    
    			// The Loop
    			if ( $query3->have_posts() )
    			{
    				while ( $query3->have_posts() )
    				{
    					$query3->the_post();
    					echo get_the_title();
    				}
    			}
    
    			/* Restore original Post Data */
    			wp_reset_postdata();

    Try that and adapt it to your needs. works, tested. You owe me 20 minutes of my life back ??

    Thread Starter wpgit

    (@wordpressgit)

    hi lee,

    cant thank you enough for this! i’ll give this a try.

    many thanks!!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘avoid featured post duplication in wordpress’ is closed to new replies.