简体   繁体   中英

woocommerce custom query string problem in filter widget

I created a widget to add query var to URL to query and filter the instock products in woocommerce shop page, the query string works and return result, but when another filter such as layered navigation or sort (eg based on lower to higher price) is selected, my custom query var (instock query, ?stockstatus=instock) removed from url, and I have to select it again, then both instock and other query strings works.

my custom href tag is in this form <a class="_widget_click" href="'. esc_url(add_query_arg('stockstatus', 'instock')). '">Show In Stock Products</a>

How can I solve this problem? any idea?

To solve this problem, you can use the add_query_arg() function to add your custom query variable to the URL, rather than replacing it. This will allow you to keep your custom query variable in the URL even when other query variables are present.

Here's an example of how you can use add_query_arg() in your code:

$url = add_query_arg( 'stockstatus', 'instock', $url );

This will add the stockstatus=instock query variable to the $url variable, preserving any other query variables that may already be present in the URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM