Forums › Forums › Search & Filter Pro › Editing query arguments
Tagged: modifying queries
- This topic has 7 replies, 2 voices, and was last updated 3 years, 10 months ago by Trevor.
-
Anonymous(Private) February 1, 2021 at 7:19 pm #274881
Hi there,
I’m trying to edit the query args to exclude posts with acf field that denotes the “event” post type is “expired” (true/false field). I’ve followed the example in the docs here …. but no dice. Not sure about the ‘somearg’ bit in your example, it is unexplained in the docs, could that be it? Many thanks in advance for having a look.function exclude_expired_results ( $query_args, $sfid ) { //if search form ID = 3988, the do something with this query if($sfid==3988) { //modify $query_args here before returning it $query_args['somearg'] = array( 'meta_query' => array( 'relation' => 'OR', array ( 'key' => 'expired', 'compare' => 'NOT EXISTS' ), array ( 'key' => 'expired', 'value' => '1', 'compare' => '!=' ) ) // end meta_query ); // end $args } return $query_args; } add_filter( 'sf_edit_query_args', 'exclude_expired_results', 20, 2 );
Trevor(Private) February 2, 2021 at 7:20 am #274906somearg
would meta_query I think.$query_args['meta_query'] = array( 'relation' => 'OR', array ( 'key' => 'expired', 'compare' => 'NOT EXISTS' ), array ( 'key' => 'expired', 'value' => '1', 'compare' => '!=' ) ); // end meta_query addtion to args
You are ADDING to the wp_query arguments, not setting them new. I cannot be certain that code is right.
Trevor(Private) February 4, 2021 at 8:01 pm #275341You can add our query to pre_get_posts like this:
function pre_get_posts_function($query) { //this would be a pre_get_posts you already have in place somewhere //then set <code>search_filter_id</code> $query->set("search_filter_id", 123); } add_action( 'pre_get_posts', array($this, 'pre_get_posts_function') );
Anonymous(Private) February 7, 2021 at 10:36 pm #275486Hmm. This throws an error…
“Uncaught Error: Using $this when not in object context”And when I change
add_action( 'pre_get_posts', array($this, 'pre_get_posts_function') );
to
add_action( 'pre_get_posts', 'pre_get_posts_function' );
Error is WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘-1 /* From [url/?sfid=3988&_sft_services-and-experiences=’ at line 5]
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id AND mt1.meta_key = ‘expired’ ) WHERE 1=1 AND wp_posts.ID IN (4992,4993,4995,5228,5229,5230,5278,5279,5280,5290,5291,5367,5454,5455,5456,5457,5458,5459,5460,5461,5462,5463,5464,5465,5466,5467,5468,5469,5470,5471,5484,5501,5503,5504,5505,5506,5508,5510,5517,5519,5554,5555,5556,5557,5558,5559,5560,5562,5563,5564,5565,5566,5567,5568,5569,5570,5571,5572,5573,5574,5575,5576,5595,5596,5597,5598,5680,5681,5682,5683,5684,5685,5686,5828,5829,5830,5831,5832,5833,5834,5842,5844,5846,5847,5848,5849,5850,5917,5918,5920,5921,5950,5951,5952,5953,5954,6009,6010,6011,6012,6013,6014,6015,6016,6017,6018,6019,6020,6021,6022,6023,6024,6025,6026,6027,7140,7167,7168,7196,7199,7201,7203,7204,7205,7206,7207,7209,7229,7262,7289,7290,7318,7323,7324,7325,7326,7327,7328,7329,7348,7320,7321,7322) AND ( ( wp_postmeta.meta_key = ‘expired’ AND wp_postmeta.meta_value = ‘0’ ) OR mt1.post_id IS NULL ) AND wp_posts.post_type IN (‘experiences’, ‘event’) AND ((wp_posts.post_status = ‘publish’)) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value ASC LIMIT 0, -1 /* From [clusiveevents.wpengine.com/?sfid=3988&_sft_services-and-experiences=dance] in [/nas/content/live/clusiveevents/wp-content/plugins/search-filter-pro/public/class-search-filter.php:327] */thoughts?
Anonymous(Private) February 9, 2021 at 4:52 pm #275584Sure. So I want the results here to display the same as the taxonomy archive page.
Here is the pre_get_post that results in the taxonomy archive page.
// Order posts first by event and date, then by other kinds of content function order_events_by_event_date($query=false) { // only used on main query on front end of site if (is_admin() || !$query || !$query->is_main_query()) { return; } // modfiy a custom post type to show and order by meta value if (is_tax() || is_archive() ) { $query->set('orderby', array( 'meta_value' => 'DESC' ) ); $query->set('order', 'ASC'); $query->set('posts_per_page', -1); $meta_query = array( 'relation' => 'OR', array( 'key' => 'expired', 'compare' => 'NOT EXISTS', ), array( 'relation' => 'OR', array( 'key' => 'expired', 'value' => '0', ), array( 'key' => 'expired', 'value' => '1', 'compare' => '!=', ), ), ); $query->set('meta_query', $meta_query); } } // end function blunt_pre_get_posts add_action('pre_get_posts', 'order_events_by_event_date');
The nested array is necessary to include the “experiences” post type (first OR) and the array beneath it includes items that are not expired or field expired is empty.
I changed it using the code you provided above, removing the is_tax and first qualifiers about queries, and got the results posted above.
Only thing that seems to semi-sort the results is the sfid filter, so what is displaying results now is
function exclude_expired_results ( $query_args, $sfid ) { //if search form ID = 3988, the do something with this query if($sfid==3988) { //modify $query_args here before returning it $query_args['meta_query'] = array( 'orderby' => array( 'meta_value' => 'DESC' ), 'order' => 'ASC', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'expired', 'compare' => 'NOT EXISTS', ), array( 'relation' => 'OR', array( 'key' => 'expired', 'value' => '0', ), array( 'key' => 'expired', 'value' => '1', 'compare' => '!=', ), ), ), ); // end meta_query addtion to arg } return $query_args; } add_filter( 'sf_edit_query_args', 'exclude_expired_results', 20, 2 );
sfid is 3988. Thank you for any help.
-
AuthorPosts