TransWikia.com

Wpdb result with pagination

WordPress Development Asked by Dab Patoel on December 10, 2021

i got this function and its working properly without pagination, but right now i need some pagination on page as a listing is grow up now, how can i do that?

function listing_items($args){
    global $wpdb;

    $listQ = new WP_Query( $args );
    $pid = get_the_ID( );

    $return = '';

    if ( $listQ->have_posts() ) {

        while ( $listQ->have_posts() ) {
            $listQ->the_post();
            $listing = $wpdb->get_results( "SELECT * FROM wp_listings WHERE post_id =".get_the_ID() );
            echo $pid;
            $summary = $listing[0]->home_description;
            $excerpt = wp_trim_words( $summary, $num_words = 25, $more ='.....' );

            $return .= '<div class="listing-item" data-location="lat: '.$listing[0]->lat.', lng: '.$listing[0]->lng.'" data-lat="'.$listing[0]->lat.'" data-lng="'.$listing[0]->lng.'">';

            $return .= '<div class="listing-image"><a href="'.esc_url( get_post_permalink() ).'">';
            if( $listing[0]->image == '' ){
                $return .= '<img src="http://via.placeholder.com/300x300" alt="" />';
            } else {
                $return .= '<img src="'.home_url().'/media/listings/'.get_the_ID().'/thumb_'.$listing[0]->image.'" alt="" />';
            }
            $return .= '</a></div>';

            $return .= '<div class="listing-details">';
            $return .= '<h3 class="listing-title"><a href="'.esc_url( get_post_permalink() ).'">'.$listing[0]->title.'</a></h3>';
            $return .= '<div class="listing-detail">'.$listing[0]->accommodates . ' guests<span class="middot">·</span>' .$listing[0]->bedrooms . ' bedrooms<span class="middot">·</span>' .$listing[0]->bathrooms . ' bathrooms</div>';
            $return .= '<div class="listing-excerpt">' .$excerpt.'</div>';
            $return .= '<div class="listing-location"><a class="btn-listing" href="'.esc_url( get_post_permalink() ).'">Read More</a></div>';
            $return .= '</div>';
            $return .= '</div>';
        }

        /* Restore original Post Data*/
        wp_reset_postdata();

        return $return;

    } else {
        echo '<p class="no-response">No Listing yet here</p>';
    }
}

2 Answers

@mozboz, i have same function mostly same like @dab patoel function, and i follow what you said and the result i can get pagination page when add $args['posts_per_page'] = 2 i put the $next_posts = get_next_posts_link(); but it seem not work, how can i get link to next result working?

Answered by Agustinus on December 10, 2021

Your inner query looks like it just gets a single item from wp_listings, so I assume you mean pagination on the outer WP_Query ?

This is described here:

https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters

So to get the 4th page, with 20 posts per page you need this:

$args['posts_per_page'] = 20;
$args['paged'] = 4;
$listQ = new WP_Query( $args );

There's more info on pagination including how you can render pagination links here:

https://developer.wordpress.org/themes/functionality/pagination/

Answered by mozboz on December 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP