0

I have a woocommerce products per page drop down. It works perfectly but when applied upon search results, it breaks and I use relevanssi for search, here is my code for the drop down/select in my functions.php:-

add_action('woocommerce_before_shop_loop','my_select',25);

function my_select(){
    $per_page = filter_input(INPUT_GET,'perpage',FILTER_SANITIZE_NUMBER_INT);

    echo "<form class='woocommerce-perpage' method='get'><span>Products per Page:</span>";
    echo "<select onchange='this.form.submit()' name='ga_select_ppp'>";
    $orderby_options = array(''=>'','40'=>'40','60'=>'60');
    foreach($orderby_options as $key=>$value){
        echo "<option  value='$value'>$value</option>";

    }
    echo "</select></form>";
}

add_action('pre_get_posts','ga_pre_get_products_query');
function ga_pre_get_products_query($query){
    $per_page = filter_input(INPUT_GET,'ga_select_ppp',FILTER_SANITIZE_NUMBER_INT);
    global $wp_query;
    if($query->is_main_query() && !is_admin()  ){
        $query->set('posts_per_page',$per_page);   

    } 

}

This works perfectly for anything that doesn't involve the search parameter in the URL. For example, in this page URL, example.com/?s=test&post_type=product upon clicking the drop down for 60 pages instead of example.com/?s=test&post_type=product&ga_select_ppp=60 it goes to this URL example.com/?ga_select_ppp=60 and 60 products per page aren't displayed. So, what relevant hooks and filters of relevanssi needs to be used and how?Thanks

YeshDev
  • 63
  • 13

1 Answers1

0

The form only sends the ga_select_ppp parameter, and nothing else. That's why the s and post_type parameters are not passed.

Changing the my_select() function to this should work better:

function my_select(){
    $s         = filter_input(INPUT_GET,'s',FILTER_SANITIZE_STRING);
    $post_type = filter_input(INPUT_GET,'post_type',FILTER_SANITIZE_STRING);

    echo "<form class='woocommerce-perpage' method='get'><span>Products per Page:</span>";
    if ($s) {
        echo "<input type='hidden' name='s' value='$s' />";
    }
    if ($post_type) {
        echo "<input type='hidden' name='post_type' value='$post_type' />";
    }
    echo "<select onchange='this.form.submit()' name='ga_select_ppp'>";
    $orderby_options = array(''=>'','40'=>'40','60'=>'60');
    foreach($orderby_options as $key=>$value){
        echo "<option  value='$value'>$value</option>";

    }
    echo "</select></form>";
}

Also, you can simplify this a bit – at least for search you don't need the pre_get_posts filter at all, if you just name the ga_select_ppp input to posts_per_page. Relevanssi understands http://www.example.com/?s=test&post_type=product&posts_per_page=60 quite well.

Mikko Saari
  • 160
  • 1
  • 9
  • Thank You. It works perfectly, but I have to use pre_get_posts filter maybe because of my relevanssi version or just that it's somewhere else in my code. – YeshDev Jan 10 '19 at 15:55
  • Relevanssi certainly doesn't need it, if you just name the `select` directly as `posts_per_page`. – Mikko Saari Jan 11 '19 at 03:27
  • Hi Mikko, right now i'm working on an infinite scroll .I tried implementing it using jet pack but with serach it doesn't work and i came across this https://www.relevanssi.com/knowledge-base/infinite-scroll-jetpack/. Is there any change in relveanssi's support to infinite scroll and to be more exact my issue is this https://stackoverflow.com/questions/54226941/applying-infinite-scroll-upon-wooocmmerece-site-isnt-working-as-expected. Thanks – YeshDev Jan 16 '19 at 23:42