0

Based upon this I have tried to write the following code as:

Custom Page template:

    /*
 Template Name: product_filter
     */
    get_header();
    do_action( 'pre_get_posts', 'COOLING');

Functions.php

add_action('pre_get_posts','shop_filter_cat',10,1);

 function shop_filter_cat($query, $arg) {
    if (!is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query()) {
       $query->set('tax_query', array(
                    array ('taxonomy' => 'product_cat',
                                       'field' => 'slug',
                                        'terms' => $arg
                                 )
                     )
       );   
    }
 }

But I have got following warning:

Warning: Missing argument 2 for shop_filter_cat(), called in E:\xampp\htdocs\wordpress\wp-includes\plugin.php on line 600 and defined in E:\xampp\htdocs\wordpress\wp-content\themes\storefront\functions.php on line 56

and error:

Fatal error: Uncaught Error: Call to a member function is_main_query() on string in E:\xampp\htdocs\wordpress\wp-content\plugins\woocommerce\includes\wc-deprecated-functions.php:677 Stack trace: #0 E:\xampp\htdocs\wordpress\wp-includes\plugin.php(524): wc_shop_order_status_backwards_compatibility('COOLING') #1 E:\xampp\htdocs\wordpress\wp-content\themes\storefront\testing.php(6): do_action('pre_get_posts', 'COOLING') #2 E:\xampp\htdocs\wordpress\wp-includes\template-loader.php(75): include('E:\xampp\htdocs...') #3 E:\xampp\htdocs\wordpress\wp-blog-header.php(19): require_once('E:\xampp\htdocs...') #4 E:\xampp\htdocs\wordpress\index.php(17): require('E:\xampp\htdocs...') #5 {main} thrown in E:\xampp\htdocs\wordpress\wp-content\plugins\woocommerce\includes\wc-deprecated-functions.php on line 677

3

you make a mistake in the call of add_action
the last parameters must be changed from 1 to 2

add_action('pre_get_posts','shop_filter_cat',10,2);
  • 2
    the do_action is probably not needed as well – Mark Kaplun Oct 9 '16 at 9:28
  • ok, it removed warning and what about error – Muhammad Muazzam Oct 9 '16 at 9:36
  • MuhammadMuazzam: I think @MarkKaplun already answered that for you regarding the error, since you're using string as an input argument. There's also no second input argument for pre_get_posts by default. – birgire Oct 9 '16 at 10:18
  • do_action is being used to change parameter value when custom page input field value change that I will do as well. so when I have tried to change it as do_action( 'pre_get_posts', '','COOLING'); it still have error – Muhammad Muazzam Oct 9 '16 at 10:51
  • @MuhammadMuazzam This is not the way to pass on data to the main query, since you're overriding the query object with a string value (hence the error) and the main query has already been activated when you are inside the template file. – birgire Oct 9 '16 at 12:07

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.