1

I have almost zero knowledge with Wordpress and Wocoommerce cores and api's so, please bear with me.

I have showing ~140 products on custom page (e.g. not the default shop page for woocommerce) trough WooCommerce Product Filter.

The products are on page and everything is fine except there is no way to show dropdown Products per page which I can choose how many products to show on page.

So, I've found some tutorials and currently I have this in my function.php

add_action( 'woocommerce_before_shop_loop', 'ps_selectbox', 25 );
function ps_selectbox() {
    $per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);     
    echo '<span>Per Page: </span>';
    echo '<select onchange="if (this.value) window.location.href=this.value">';   
    $orderby_options = array( '8' => '8', '16' => '16','32' => '32','64' => '64' );
    foreach( $orderby_options as $value => $label ) {
        echo "<option ".selected( $per_page, $value )." value='?perpage=$value'>$label</option>";
    }
    echo '</select>';
}
add_action( 'pre_get_posts', 'ps_pre_get_products_query' );
function ps_pre_get_products_query( $query ) {
    $per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
    if( $query->is_main_query() && !is_admin() && is_page() ) {
        $query->set( 'posts_per_page', $per_page );
    }
}

function render_ps_sel() {
    return ps_selectbox();
}
add_shortcode( 'ps_selectbox', 'render_ps_sel' );

I'm render the dropdown menu trough the shortcode above [ps_selectbox] but whatever I choose the number of the products on the page is always same and nothing change.

  • First thing I noticed is that there appears to be a typo on row 3 for $per_page as the second parameter for filter_inputis perpagee and on row 14 it's just perpage. – Antti Koskinen Mar 25 at 13:14
  • @AnttiKoskinen it's fixed. Just copied wrong snipped. Edited in question too. – Jordan Mar 25 at 13:17
  • I updated the code example in my answer, if you want to give it a try. – Antti Koskinen Mar 25 at 14:06
1

Maybe this would work?

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

    if ( ! is_admin() && is_woocommerce() && is_page() ) {
      $query->set( 'posts_per_page', $per_page );
    }
}

I tested your code and the if statement didn't seem to work with is_page() (but that could also be because my local WP sandbox is a mess). To my knowing posts_per_page expects int and var_dump showed that $per_page was a string.

EDIT My local WP sandbox is a mess, so that's why is_page wasn't working. I tested the code again on another install.

EDIT 2 Let's see how many times I can get this wrong. I updated the code to something that works (at least on another local WP sandbox).

  • Thanks for the answer. I have tested it but sadly it didn't work on my end. I don't know why. The products are set to 10 by default on the WooCommerce Product Filter. I've select 64 from the dropdown. The page refreshed and the products are still 10. – Jordan Mar 25 at 14:17

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.