Home > other >  Wordpress - two loops in CPT archive page, exclude posts from main query
Wordpress - two loops in CPT archive page, exclude posts from main query

Time:11-16

I have two loops in my archive-inzerat.php, first is custom loop, second is default loop:

<div >
    <div >
        
        <?php
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        
        $featured_args = array(
            'post_type' => 'inzerat',
            'post_status' => 'publish',
            'posts_per_page' => 10,
            'paged' => $paged,
            'orderby' => 'publish_date', 
            'order' => 'DESC',
            'meta_query' => array(
                array(
                    'key'   => 'is_featured',
                    'value' => '1',
                )
            )
        );
        $featured_query = new WP_Query( $featured_args );
        
        if ( $featured_query->have_posts() ) : ?>   
        
            <div >                     
                <div >

                    <?php
                    while ( $featured_query->have_posts() ) : $featured_query->the_post();

                        echo '<div >';
                        get_template_part( 'content', 'inzerat' );
                        echo '</div>';

                    endwhile;
                    wp_reset_postdata();
                    ?>

                </div>
            </div>
        
        <?php
        endif;
        ?>
        
        <?php
        if ( have_posts() ) : ?>    
        
            <div >                     
                <div >

                    <?php
                    while ( have_posts() ) : the_post();

                        echo '<div >';
                        get_template_part( 'content', 'inzerat' );
                        echo '</div>';

                    endwhile;
                    ?>

                </div>

                <div ></div>

                <div >
                    <nav >
                        
                        <?php
                        global $wp_query;
                        $total_pages = $wp_query->max_num_pages;

                        if ( $total_pages > 1 ) {

                            $current_page = max( 1, get_query_var( 'paged' ) );

                            echo paginate_links( array(
                                'base' => get_pagenum_link( 1 ) . '%_%',
                                'format' => '/page/%#%',
                                'current' => $current_page,
                                'total' => $total_pages,
                                'prev_text' => '<i ></i>',
                                'next_text' => '<i ></i>',
                                'type'      => 'list',
                                'end_size'  => 3,
                                'mid_size'  => 3,
                            ) );
                        }

                        wp_reset_postdata();
                        ?>

                        <script>
                            jQuery('ul.page-numbers').addClass('pagination-custom');
                            //jQuery('ul.page-numbers li').addClass('page-item');
                            //jQuery('ul.page-numbers li a, ul.page-numbers li span').addClass('page-link');
                            jQuery('ul.page-numbers li span.current').parent().addClass('active');
                        </script>                       
                    </nav>
                </div>
            </div>
        
        <?php
        endif;
        ?>
        
    </div>              
</div>

In first loop I want to display only custom posts by custom field is_featured which is type boolean. In second loop I want to exclude these posts and display others to prevent duplications. Unfortunately, this code I use removes everything from the main loop:

function my_posts_filter( $query ) {
    if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'inzerat' ) ) {       
        
    $query->set( 'meta_query', array(

        array(
              'key' => 'is_featured',
              'value' => '1',
              'compare' => 'NOT IN'
        )

    ));
        
    }
}
add_action( 'pre_get_posts', 'my_posts_filter' );

What's wrong here?

I want also ask, will my pagination work and not break when adding custom loop? Thanks for helping.

CodePudding user response:

Try with post__not_in argument:

$featured_args = array(
            'post_type' => 'inzerat',
            'post_status' => 'publish',
            'posts_per_page' => 10,
            'paged' => $paged,
            'orderby' => 'publish_date', 
            'order' => 'DESC',
            'post__not_in' => array(278),
            'meta_query' => array(
                array(
                    'key'   => 'is_featured',
                    'value' => '1',
                )
            )
        );

It will be definitely exclude specific posts.

CodePudding user response:

As per my opinion try this logic, Define a blank array at the beginning of your file.

$firstloop_ids = array();

When your first loop is performed, you just need to add those IDs into that blank array. ( you can use array_push for that, reference: https://www.w3schools.com/php/func_array_push.asp )

For the second loop, you can exclude those ids using that array by this parameter.

'post__not_in' => $firstloop_ids,

This should work for you!

  • Related