0

I am stuck on an issue for the last two days. I have a CPT - 'listings'. This is it's archive page

  • Till here, everything is fine.

  • I put in a search form there, and added this to pre_get_posts:

    if ($query->is_main_query() && $query->is_post_type_archive('listings') &&  !is_admin() && is_search()) {
    
    echo '<br/>me runnning<br/>';
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query->set('paged', $paged);
    
    $query->set('posts_per_page', 1);
    
  • Another code in 'functions.php' that redirects my search to 'archive_listings.php', which is the same archive page for 'listings' CPT

  • When I do a search the results that appears are fine.

Problem arises when I try to get on the second page in the search results.

  • When I click on next page for search results, it takes me to the

wwwsitename.com/listings/page/2

which is the page of archive-listings default results.

  • In other words, when I try to move to next page - the query forgets about the search terms, and get the second page of default archive for listings.

I am not sure, if I was able to clearly explain it. So, I haveprint_r($wp_query) which can help understand the issue

I have tried several code blocks from support forums, stack exchange, and other blogs.

Any suggestions will be heartily appreciated.

4
  • Remove $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $query->set('paged', $paged); from your pre_get_posts function Commented Jul 20, 2014 at 6:31
  • I tried just removing that $paged manipulations, but no success.
    – OmAk
    Commented Jul 20, 2014 at 13:48
  • Now, my pre_get_posts only contains a query->set to change posts_per_page=>1. So search results should show only 1 listing per page. Which it does right on first page. One second it just take me to the page/2 for listings archive.
    – OmAk
    Commented Jul 20, 2014 at 14:08
  • You can try searching for "a" in the search box. It will return 17 listings and posts_per_page is 1, and when going on page/2 it will show 18 listings and posts_per_page goes to default 4. Which is also set in the pre_get_posts for is_post_type_archive('listings')
    – OmAk
    Commented Jul 20, 2014 at 14:09

1 Answer 1

0

You are posting the query to archive_listings. Either you need to use get method and retain the search query in URL or you should store the search query in session and handle the page results if there is a query variable present.

5
  • I can theoretically understand what do you mean to say and this is the actual problem. My search seems to work fine, as I have used the default id="s" for the input field. The results come fine, when going to page to it reloads the default query for archive-listings. This sounds obvious to me. As, any query with ".com/listings/page/2" will actually take me to the 2 page of listings-archive.
    – OmAk
    Commented Jul 20, 2014 at 13:48
  • Change your search form method from POST to GET, it will show the paging properly and it shows only one result in the listings as you have a hidden variable posts_per_page = 1,
    – sathishn
    Commented Jul 20, 2014 at 16:17
  • Hello Sathish, This actually solved it. Can you tell me just for the knowledge part, why $_POST didn't work here. I can see the string to be added in URL now. I actually have tried several-several things for this. But, just could not think of this POST vs GET thing. I really appreciate you looked down into the code, and found this.
    – OmAk
    Commented Jul 20, 2014 at 16:40
  • URL will have the string posted and from second page this string will be retained
    – sathishn
    Commented Jul 20, 2014 at 16:47
  • Can you mark the answer as correct answer
    – sathishn
    Commented Jul 20, 2014 at 16:48

Your Answer

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

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