Bricks/posts/query_vars and Ajax

I’m using the

add_filter( ‘bricks/posts/query_vars’, function( $query_vars, $settings, $element_id )

In this filter I have

if ( isset( $_GET[‘_orderby’] ) && $_GET[‘_orderby’] == ‘menu_order_ASC’ )

My code executes properly on page loads, but for my bricks ajax filter, it skips the $_GET[‘_orderby’] fucntion because the filter is using ajax…

How can I solve this? Am I using the wrong filter?

Hello @dailce,

the reason why $_GET[‘_orderby’]is not working, it’s because for filtering, the request is a type of POST + JSON data.

It depends what you need that filtering data for, and what you do with it, but I see two options:

  1. You can use \Bricks\Query_Filters::$active_filters to access current active filters

or

  1. You can increase the priority of your filter to 1000 (or more), and then, the &query_vars will be already merged with the filter, and you will have access to the _orderby part of the query vars. But, you have to be careful, if you will be modifying them, as this will run after merging query vars of filters, so whatever you do, will be the final query_vars.

I’ll also move the topic to How To, as it’s not a bug.
I hope it helps :slight_smile:

Matej

I went with this and it works:

// Read raw POST data (JSON) from php://input
$json_data = file_get_contents('php://input');

// Decode the JSON data into a PHP array
$data = json_decode($json_data, true);

// Get the 'orderby' value from the GET request
$orderby_get = isset($_GET['_orderby']) ? $_GET['_orderby'] : '';

// Get the 'selectedFilters' from the decoded JSON POST data
$orderby_post = isset($data['selectedFilters']) ? $data['selectedFilters'] : [];
1 Like