Loading

WordPress Custom Post Type Pagination

The last couple of days have been very hectic for me for finding a solution regarding how to write code for multiple meta queries for custom fields, and as I am a great fan of ACF(Advanced Custom Fields) I prefer using ACF because it’s very easy to create fields and display values on front-end side.

In my last project, my task was to display results based on the search with pagination. I had so much googled and posted a question on Stackoverflow and WordPress forums but did not get any proper answer. Then I decided to use $wpdb SQL query.

Why I did not choose meta_query? Because when I pass more than 3 keys and value to search, my page kept loading and loading.

Before I explain more about this article, here I am posting my meta_query code for readers. If you know how to fix this issue please share your modified code in comments and I will add in my next post.

<?php   
    $args = array(
        'post_type' => 'book',
        'post_status'   => 'publish',
        'order' => 'DESC',
        'orderby' => 'date',
        'posts_per_page' => -1,
        'meta_query' => array(
        'relation' => 'OR',
            array(
                'key' => 'type',
                'value' => 'Sample 1',
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'type',
                'value' => 'Sample 2',
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'type',
                'value' => 'Sample 3',
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'type',
                'value' => 'Sample 4',
                'compare' => 'LIKE'
            )
        )
    );
    $query = new WP_Query( $args );
?>

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

    <h1><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h1>

<?php endwhile; endif; wp_reset_postdata(); ?>

Finally, I found the solution from StackOverflow and would like to thanks everyone who posted the answer. Now I am presenting that code with little bit modification by me. In this code, your task is only to change the SQL query to your query.

<?php      
    global $wpdb, $paged, $max_num_pages;

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $post_per_page = 3;
    $offset = ($paged - 1)*$post_per_page;

    $query_spicy = "SELECT wposts.* FROM $wpdb->posts wposts WHERE wposts.post_status = 'publish' AND wposts.post_type = 'villa' ORDER BY wposts.post_date DESC";

    //QUERY THE POSTS WITH PAGINATION
    $spicy = $query_spicy . " LIMIT ".$offset.", ".$post_per_page."; ";

    $spicy_results = $wpdb->get_results( $spicy, OBJECT);

    //RUN QUERY TO COUNT THE RESULT LATER
    $total_result = $wpdb->get_results( $query_spicy, OBJECT );

    $total_spicy_post = count($total_result);
    $max_num_pages = ceil( $total_spicy_post / $post_per_page );
?>

<div class="result-count">
        <?php 
            if($total_spicy_post >= 1){
                ?> Showing all <?php echo $total_spicy_post; ?> results <?php
            }else{
                ?> No records found. <?php
            }
        ?>
</div>

<?php
    global $post;
    foreach ($spicy_results as $post) :
        setup_postdata($post);
?>
    <h1><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h1> 
<?php
    endforeach;
?>

<?php
    global $wp_rewrite, $wp_query, $max_page, $page;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

    $pagination = array(
        'base' => @add_query_arg('page','%#%'),
        'format' => '',
        'total' => $max_num_pages,
        'current' => $current,
        'prev_text' => __('PREV'),
        'next_text' => __('NEXT'),
        'end_size' => 1,
        'mid_size' => 2,
        'show_all' => false,
        'type' => 'list'
    );

    if ( $wp_rewrite->using_permalinks() )
           $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );

    if ( !empty( $wp_query->query_vars['s'] ) )
            $pagination['add_args'] = array( 's' => get_query_var( 's' ) );

    echo paginate_links( $pagination );
?>

Keep visiting for new stuff andĀ give your feedback.

Happy Coding šŸ˜‰

%d bloggers like this: