skip to Main Content

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

<div class="container">
    <div class="row">
        
        <?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 class="col-md-12">                     
                <div class="row">

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

                        echo '<div class="col-md-3">';
                        get_template_part( 'content', 'inzerat' );
                        echo '</div>';

                    endwhile;
                    wp_reset_postdata();
                    ?>

                </div>
            </div>
        
        <?php
        endif;
        ?>
        
        <?php
        if ( have_posts() ) : ?>    
        
            <div class="col-md-12">                     
                <div class="row">

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

                        echo '<div class="col-md-3">';
                        get_template_part( 'content', 'inzerat' );
                        echo '</div>';

                    endwhile;
                    ?>

                </div>

                <div class="clearfix"></div>

                <div class="utf-pagination-container margin-top-20">
                    <nav class="pagination">
                        
                        <?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 class="fa fa-angle-left"></i>',
                                'next_text' => '<i class="fa fa-angle-right"></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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search