• Hi i have a page template with this custom query on a not wordpress db table:

    global $wp_query, $wp_rewrite, $wpdb, $paged, $max_num_pages;
    
    $comune    = (get_query_var('comune'))?get_query_var('comune'):"";
    $categoria = (get_query_var('categoria'))?get_query_var('categoria'):"";
    $localita  = (get_query_var('localita'))?get_query_var('localita'):"";
    
    $query = 'SELECT * FROM Annunci ';
    if ($categoria == 'residenziale') {
                        $query .= ' WHERE (Categoria = "'.$categoria.'" OR Categoria = "nuovo")';
                    } else {
                        $query .= ' WHERE Categoria = "'.$categoria.'"';
                    }
                    if ($comune) {
                        $query .= ' AND UPPER(Comune) = UPPER("'.$comune.'")';
                    }
                    if ($localita) {
                        $query .= ' AND UPPER(Localita) = UPPER("'.$localita.'")';
                    }
    $post_per_page = 10;
                    $offset        = ($paged-1)*$post_per_page;
                    $queryOffset   = $query." LIMIT ".$offset.", ".$post_per_page;
                    $result        = $wpdb->get_results($queryOffset);
                    $total_result  = $wpdb->get_results($query);
                    $max_num_pages = ceil(Count($total_result)/$post_per_page);
    .....

    and it works well for the first page, now i added a pagination section:

    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
            //$paged = (empty($wp_query->query_vars['paged'])) ? 1 : $wp_query->query_vars['paged'];
    
            $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 );

    it shows the list with page number on the bottom, when i click one of this links, the page that appear has no results, i have printed on video the query vars array, and on second(or any other page) it miss all the custom query_vars of my page (see first block of code). How can i solve this?

    thanks

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘custom query pagination with query_args’ is closed to new replies.