Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › asp_query_args & post_in
- This topic has 9 replies, 2 voices, and was last updated 4 years, 7 months ago by
willfaulds59.
-
AuthorPosts
-
October 18, 2021 at 5:21 pm #35204
willfaulds59
ParticipantHello,
I’m loving ASP.
I’m working with WooCommerce and want to create links to very specific dynamically lists of products (specified by post ids).
I thought I was on to a winner with https://knowledgebase.ajaxsearchpro.com/hooks/filters/query-and-output/asp_query_args#post_in-posts-by-ids.
But I cannot work out a way to get access to the query string in the filter. Most probably because it is being called by AJAX…
$_REQUEST shows:
Array ( [action] => ajaxsearchpro_search [aspp] => [asid] => 1 [asp_inst_id] => 1_1 [options] => current_page_id=32&woo_currency=GBP&qtranslate_lang=0&filters_changed=0&filters_initial=1&asp_gen%5B%5D=title&asp_gen%5B%5D=content&asp_gen%5B%5D=excerpt&customset%5B%5D=product&force_count=10&force_order=1 [autop] => 1 [woocommerce-login-nonce] => [_wpnonce] => [woocommerce-reset-password-nonce] => )Have you any idea how I can set a list of post_in ids for the initial (onLoad only) results dynamically via the page query string?
-
This topic was modified 4 years, 7 months ago by
willfaulds59.
October 18, 2021 at 5:57 pm #35207willfaulds59
ParticipantI can see a potential method… but can’t get it working.
The $_REQUEST [options] contains some of the query string.
I have tried registering a custom query variable
add_filter( 'query_vars', 'add_query_vars_filter' ); function add_query_vars_filter( $vars ){ $vars[] = 'custom_var'; return $vars; }but ASP but not pass it to the asp_query_args filter/AJAX call
I have tried adding a custom ASP front-end filter
add_action('asp_pre_parse_filters', 'asp_add_my_own_filters', 10, 2); function asp_add_my_own_filters($search_id, $options) { $filter = wd_asp()->front_filters->create( 'custom_field', 'Text stock', // text or hidden 'text', array( 'field' => '_stock_status', // See operator list on above example 'operator' => 'eq' ) ); $filter->add(array( 'label' => 'Stock status', 'value' => 'instock', 'default' => 'instock' )); $filter->selectByOptions($options); wd_asp()->front_filters->add($filter); }but I cannot override this via the query string…
Any ideas as to a viable solution?
-
This reply was modified 4 years, 7 months ago by
willfaulds59.
October 18, 2021 at 7:00 pm #35209willfaulds59
ParticipantAnother dead end 🙁
add_filter("asp_query_args", "asp_query_args_change", 10, 2); function asp_query_args_change($args, $search_id) { //WORKAROUND:set a search term? // $args[s]=searchterm // &asp_s='+encodeURIComponent('related_items:') // &asp_s=related_items%3A //as this is loaded via AJAX I cannot access the query string, $_REQUEST doesn't hold the referer URL either :( if($args['s']) { $searchterm = $args['s']; if ( strpos( $searchterm, 'related_items:' ) !== false) { $id_string = str_replace('related_items:', '', $searchterm); $ids = explode(',',$id_string); $args['post_in'] = array_unique( $ids ); $args['s']=''; } } //TODO stop the search term showing in search box return $args; }I thought I had a great workaround but unfortunately the $args[‘post_in’] seems to only apply to standard WP posts?
Any better solutions/imporvements for WC products?
October 19, 2021 at 9:59 am #35210Ernest Marcinko
KeymasterHi,
The problem with this is, that the separate pageview (in this case ajax request) can not see the previous pageview query arguments. To achieve that, it has to be passed on. I think the easiest way is this:
add_action('asp_layout_in_form', 'asp_layout_in_form_add_input'); function asp_layout_in_form_add_input() { ?> <input type="hidden" name="my_custom_variable" value="<?php echo esc_attr($_GET['your_variable']); ?>"> <?php } add_filter( 'asp_query_args', 'asp_change_some_variables', 10, 2 ); function asp_change_some_variables( $args, $id ) { if ( isset($_POST['options']) ) { parse_str($_POST['options'], $options); if ( isset($options['my_custom_variable']) ) { $ids = explode(',', $options['my_custom_variable']); $args['post_in'] = array_unique( array_merge($args['post_in'], $ids) ); } } return $args; }The first action is executed, whenever the search bar settings form is printed. There, the original $_REQEST, $_GET, $_POST variables will contain the values from the initial pageview. You can use that hook to print additional input fields to the form, which will get passed on to the ajax request.
In the example above, I passed the value from the
$_GET['your_variable']query string to the “my_custom_variable” input field. This is serialized into the$_POST['options']variable, which can be decoded via theparse_str($_POST['options'], $options);call. From there the$options['my_custom_variable']will contain the string value you passed to the original page.October 19, 2021 at 12:55 pm #35213willfaulds59
ParticipantAha, yes that’s a good solution to passing the variables.
Do you have a recommendation for a similar functionality to the post_in arg for Woocommerce products?
October 19, 2021 at 2:25 pm #35214Ernest Marcinko
KeymasterThe post_in argument applies to every post type within the live search (if you mean that).
October 21, 2021 at 11:00 am #35242willfaulds59
ParticipantThank you Ernest, after debugging for a while I have found why I thought the post_in did not work for WooCommerce products.
When
$args['post_in']is set the AJAX call to admin-ajax.php response does not contain any HTML – its a data JSON only. I will copy into a private reply the Headers and Response from Chrome Dev tools.-
This reply was modified 4 years, 7 months ago by
willfaulds59.
-
This reply was modified 4 years, 7 months ago by
willfaulds59.
-
This reply was modified 4 years, 7 months ago by
willfaulds59.
October 21, 2021 at 11:07 am #35246willfaulds59
ParticipantYou cannot access this content.
October 22, 2021 at 9:53 am #35271willfaulds59
ParticipantI have noticed some missing results elsewhere so I’m going to debug the issue today and see if its being caused by a modification/override/plugin.
October 26, 2021 at 1:08 pm #35345willfaulds59
ParticipantThanks for you’re help finding a working solution to passing a custom variable I have a reliable solution now.
The missing results seemed to be a weird behaviour of WooCommerce variable product types and was not specific to ASP.
-
This topic was modified 4 years, 7 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.