Forums › Forums › Search & Filter Pro › Custom WP Query using ACF value showing all filter options
Tagged: WP_Query
- This topic has 5 replies, 2 voices, and was last updated 5 years, 6 months ago by Trevor.
-
Anonymous(Private) May 9, 2019 at 11:41 am #210561
I have a custom WP Query set to grab some WooCommerce products that have an ACF True/False field set to true. I then hook this into a S&F form I created.
$args = array( 'numberposts' => -1, 'posts_per_page' => 25, 'post_type' => 'product', 'post_status' => 'publish', 'paged' => get_query_var('paged'), 'meta_key' => 'special_offer', 'meta_value' => true, ); $args['search_filter_id'] = 4672;
The S&F form itself has Taxonomy filters for pa_size and pa_color.
There are 300+ products in the store with many sizes and colours but only 2 are marked as special offer being true. The initial results that show when visiting this page reflect the 2 that I should see but the filters are displaying as if that special_offer = true requirement was not there. The total number of products shown in brackets for each filter value is well over the maximum of 2 that I should see, when any of the filters are selected it shows no results (apart from the rare occasion where the filter value I select is actually assigned to one of the 2 products that should show).
As for the S&F form settings, its set to Product and Variation with Display Results as Custom and pointing at the URL of my Special Offers page where this query runs.
Can anyone help advise what I am doing wrong here?
Thanks
Trevor(Private) May 9, 2019 at 1:32 pm #210587I changed the title for you 😉
Instead of the custom query, you may need to make the special conditions by changing OUR form query, using this filter:
https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments
Anonymous(Private) May 10, 2019 at 8:04 am #210688Thank you. So stick this in the theme functions, set to the correct form ID and add the same args per my wp_query. So I have ended up with:
function filter_function_name( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid == 4672) { //modify $query_args here before returning it $query_args['numberposts'] = -1; $query_args['posts_per_page'] = 25; $query_args['post_type'] = 'product'; $query_args['post_status'] = 'publish'; $query_args['meta_key'] = 'special_offer'; $query_args['meta_value'] = true; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
I have left the rest of the settings as per my last message (including the wp_query on the special_offers template where I am displaying the products). When I view the page the filters are not populating as expected, I should be seeing options for colour and size but I just see one colour option, which when I apply renders no results.
Am I doing something else wrong here?
Trevor(Private) May 10, 2019 at 3:48 pm #210799These:
$query_args['numberposts'] = -1; $query_args['posts_per_page'] = 25; $query_args['post_type'] = 'product'; $query_args['post_status'] = 'publish';
Should really be set in the form, not here. In the form, are you searching both Products and Variations?
Anonymous(Private) May 20, 2019 at 10:48 am #211680Sorry for the late reply. OK so I already have those query arguments in the function filter which is in functions.php. Now if I remove them from wp_query the page just has a standard WordPress loop to display content right? I have set the results method to “Custom” and defined the correct Results URL in Template Options – and yes I have the form set to display products & variations. With this setup I get no results at all. I can supply screengrabs of any sections that may help?
-
AuthorPosts