I would like to show archive custom type posts with "OR" in the query filter on Wordperss Archive.
I copied and modified the code according to the following page.
https://toolset.com/forums/topic/user-view-with-or-in-the-query-filter/
add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
// process if specific view
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 1234) ) {
if( !empty($query_args['meta_query']) ) {
$query_args['meta_query']['relation'] = 'OR';
}
}
return $query_args;
}
It works for Views, however it does not work for Wordperss Archive. Could you help?
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
To change the query on fly with WordPress archive you will have to use the WordPress hook "pre_get_posts":
- https://developer.wordpress.org/reference/hooks/pre_get_posts/
If you can share more details about what archive you created and with what filter you want to use "OR" and send me problem URL of your archive and send me admin access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Hi,
Thank you for replying.
I wrote the following code by using "pre_get_posts" instead of using query filteres on the Wordperss Archive.
As a result, it works perfect.
add_action( 'pre_get_posts', 'pre_get_posts_filter' );
function pre_get_posts_filter ( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_tax( array('taxslug1', 'taxslug2', 'taxslug3') )) {
$query->set( 'meta_query',
array( 'relation' => 'OR',
array(
'key' => 'wpcf-date1',
'value' => time(),
'compare' => '>='
),
array(
'key' => 'wpcf-date2',
'value' => time(),
'compare' => '>='
),
array(
'key' => 'wpcf-date3',
'value' => time(),
'compare' => '>='
),
array(
'key' => 'wpcf-date4',
'value' => time(),
'compare' => '>='
),
array(
'key' => 'wpcf-date5',
'value' => time(),
'compare' => '>='
),
array(
'key' => 'wpcf-date6',
'value' => time(),
'compare' => '>='
)
)
);
return;
}
}
Thank you for your support.
Koji Motoyama