Question

I'm using Genesis child theme and I am using this function to display feature posts on home page.

function modern_motoroids_extra_feature_posts(){ ?>
        <?php $do_not_duplicate = array(); ?>
        <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
        <?php query_posts( array('meta_key' => 'feature-story', 'orderby'=> 'date', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 4, 'post__not_in' => $do_not_duplicate)); ?>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <?php $do_not_duplicate[] = get_the_id(); ?>
             <?php echo get_the_titke(); ?>
        <?php endwhile; endif; ?>
        <?php wp_reset_query(); ?>

<?php }
add_action('genesis_before_content_sidebar_wrap','modern_motoroids_extra_feature_posts');

Then I am using this function to show latest posts on the same page.

function latest_feature_posts(){ ?>
        <?php $do_not_duplicate = array(); ?>
        <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
        <?php query_posts( array('orderby'=> 'date', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 10, 'post__not_in' => $do_not_duplicate)); ?>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <?php $do_not_duplicate[] = get_the_id(); ?>
             <?php echo get_the_titke(); ?>
        <?php endwhile; endif; ?>
        <?php wp_reset_query(); ?>

<?php }
add_action('genesis_before_content_sidebar_wrap','latest_feature_posts');

Problem is Its still showing duplicate posts, even after using $do_not_duplicat. I tried to search on google and stackoverflow but didn't get anything. Please help me. Whay these functions are showing duplicate posts? and how to exclude them.

Was it helpful?

Solution

EDIT: Instead of all that, you could just use a meta_query and "NOT IN" to exclude the feature stories from your second query. That's the easiest answer.

Check out this meta query documentation for help.


1, you need to have do_not_duplicate be a global array in order access it across functions (pretty sure).

2, you are overwriting it at the beginning of each function with <?php $do_not_duplicate = array(); ?>

Try taking out the part where you declare it as an array in whichever function comes 2nd. Instead echo it out to the console using this: //Debug in console echo '<script> console.dir(' . json_encode($do_not_duplicate) . '); </script>'; Hold on to that little piece of code, it has made my life so much easier for debugging variables and such.

If that doesn't work, declare the $do_not_duplicate as a global variable, and that should get the job done.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top