0

I have a custom search query that return results correctly, but when i tried to paginate the results,it shows the content of the first page. when I click on 'next'or the number of the page the search page reloads and the link changes from localhost/wordpress/search/?search_input=art&search=

to

localhost/wordpress/search/page/2/?search_input=art&search while number 1 of pages remains unclickable, enter image description here and the content of the page remains the same. I've tried documentation Codex,YouTube tutorials, StackExchange, and many other.

I have tried modifying the 'paged' argument manually by setting it to 2 then 3 and it displayed the second and 3rd pages correctly while page 1 still unclickable. by the way , i put the search query in a function, and i used a free HTML template for the site (i am in training). can anybody see where is my fault, please. HERE IS THE CODE`

            <div class="col-sm-9 padding-right">
                <div class="features_items">
                <?php 
                $book ;
                $search_text =  get_query_var('search_input');

                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $book = bbx_search($search_text,$paged);   
                $post_count = $book ->post_count;
                echo '<h2 class="title text-center">Search Results : '.$post_count .'</h2>';
                    ?>
                <?php   
                    while($book -> have_posts()){
                    $book->the_post();
                    $book_price = get_post_meta( get_the_ID(), 'book_price',true); 
                            $author_name = get_post_meta( get_the_ID(), 'author_name',true);   

                    echo '<div class="col-sm-4">';
                        echo '<div class="product-image-wrapper">';
                            echo '<div class="single-products">';
                                    echo '<div class="productinfo text-center">';
                                            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),'thumbnail' ); // or the_post_thumbnail('medium');

                                            if ($image) : 
                                                echo '<img src="'. $image[0].'" alt="" />';
                                            endif; 

                                        echo '<h2> $'.$book_price .'</h2>';
                                        echo '<p>'. the_title().'</p>';
                                        echo '<a href="'.esc_url( add_query_arg( 'book_id', get_the_ID(), site_url('/cart'))).'" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
                                    </div>
                            </div>
                        </div>
                    </div>';
                     }  ?>

                 <nav class="prev-next-posts">

                    <?php 
                    echo paginate_links(array (
                        'total'=> $book->max_num_pages
                    )); ?>
                  </nav>
                     <?php  new WP_Query(); ?>
                </div>
            </div>
        </div>
    </div>
</section>

//-----------in functions file

function bbx_search($search_text, $paged){

            $args1 = array(

                                'post_type' => 'book',
                                'post_status'=> 'publish',
                                'order'=> 'DSC',
                                'orderby' => 'publish_date',
                                'posts_per_page' => 3,
                                'paged' => $paged,
                                'meta_query' => array(
                                'relation' => 'OR',
                                    array(
                                        'key' => 'author_name', 'value' => $search_text,
                                        'type' => 'text','compare' => 'like'),
                                    array(
                                        'key' => 'book_description','value' => $search_text,
                                        'type' => 'text','compare' => 'like'),
                                    array(
                                        'key' => 'bbx_category','value' => $search_text,
                                        'type' => 'text','compare' => 'like'),              
                                    array(
                                        'key' => 'post_tag','value' => $search_text,
                                        'type' => 'text','compare' => 'like'),
                                    )

                            );

                              return new WP_Query( $args1 );

}

  • replace paged with page wordpress.stackexchange.com/a/180785/17923 – inarilo Aug 26 '17 at 19:54
  • Perhaps this answer may be of assistance to you. It demonstrates how to create a Numeric Pagination with a custom query (specifically: WP_Query). – Michael Ecklund Aug 26 '17 at 20:08
  • I did replace paged with page,still not the same – user283686 Aug 26 '17 at 20:37
  • i found something that may help,the $paged first gets the value 1, and when I press on page 2 or next it still has the same value of 1... any ideas>>> do i need to perform some check if() – user283686 Aug 26 '17 at 20:51
  • another thing, when I press on page no 2 or 3 the link changes to page/2/ and page/3/ respectively, but when I press on next it only changes to page/2/ and keeps refreshing the same page with old results. i think there is something wrong in setting the value of $paged variable – user283686 Aug 26 '17 at 21:00
1

the problem was that the get_query_var('paged')or 'page' couldn't read the value .. I don't know why? .. but it always returns 0; and the check sets $paged to 1 each time ... so I got around it, and got the page number from the URL >>> It worked for me on localhost, but I've no clue if it works on a host... for your info: I work on WordPress 4.8.1, and use PHP server WAMP 3.0.6 X64 >>> replace

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

with

$p = explode("/", $_SERVER['REQUEST_URI']); $pag = (int)($p[4]*1); if ($pag == 0 || $pag== null){$paged =1;} else {$paged=$pag;}

  • It is not the Ideal solution , but It worked perfectly. comments may help me further – user283686 Aug 27 '17 at 10:18
0

You need to pass more values to paginate_links, it says:

"Even if the whole arguments array is marked optional, the function will output nothing if you do not specify the needed arguments."

So pass all values that you don't want to use the default value for, e.g.:

echo paginate_links(array (
                            'current' => $paged,
                            'total'=> $book->max_num_pages
                    ));
  • thank @inarilo I also tried that .. not working . every thing looks alright except when I go through the pages .I think there is some thing that is resetting the query to the initial state or at least preventing the change of the $paged value.. I've no clue what it might be – user283686 Aug 26 '17 at 23:46
  • did using page not change even the results? – inarilo Aug 27 '17 at 5:16
  • no, nothing changed, results are correct.. first page contains the most recent books(custom post) related to the search keyword. the second page contains older books. but all pages shows the first page unless I pass the $paged manually by changing the code to $paged =3; but also next or page numbers changes the link while results the same – user283686 Aug 27 '17 at 7:31
  • i see you accepted my answer, so did this fix it? – inarilo Aug 30 '17 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.