This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: asp_query_args & post_in

#35210
Ernest MarcinkoErnest Marcinko
Keymaster

Hi,

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 the parse_str($_POST['options'], $options); call. From there the $options['my_custom_variable'] will contain the string value you passed to the original page.