0

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.

3
  • Is there a reason you can't just do two custom loops instead of modifying the default one?
    – Shoelaced
    Commented Nov 15, 2022 at 23:12
  • @Shoelaced Won't two custom loops ruin pagination?
    – Juraj
    Commented Nov 16, 2022 at 10:13
  • Not if you set it up correctly, however you may also be interested in WordPress's core sticky posts functionality for your specific use case.
    – Shoelaced
    Commented Nov 16, 2022 at 15:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.