I am still relatively new to WordPress. While I have extensive background in PHP, it has been somewhat maddening trying to figure out how to work with anything code related in WordPress. Often because examples will give code snippets, but not really explain where the code goes, or how it gets applies to what is shown on the front end.
That being said, I recently discovered the Code Snippets plugin, and I am starting to get a better handle on how to work with code in WordPress. To that end, I have some questions below about selecting and filtering ACF posts to display within Archives in Elementor.
I am working with a staff directory, and would like some help with code snippets for filtering the staff members.
Below is the snippet I’m working with. This snippet works exactly as expected when applied to Query ID in an Elementor block – it returns staff members who have the active field set to Active.
function active_staff( $query ) {
$query->set( ‘post_type’, ‘staff’ );
$query->set(‘meta_key’, [‘active’]);
$query->set(‘meta_value’, [‘Active’]);
}
add_action( ‘elementor/query/active_staff’, ‘active_staff’ );
It also works as expected if I filter by the department.
function fiscal( $query ) {
$query->set( ‘post_type’, ‘staff’ );
$query->set(‘meta_key’, [‘department’]);
$query->set(‘meta_value’, [‘Fiscal’]);
}
add_action( ‘elementor/query/fiscal’, ‘fiscal’ );
I can also filter the meta\_value for multiple values – Fiscal and Administrative in this example.
function department( $query ) {
$query->set( ‘post_type’, ‘staff’ );
$query->set(‘meta_key’, [‘department’]);
$query->set(‘meta_value’, [‘Fiscal’, ‘Administration’]);
}
add_action( ‘elementor/query/department’, ‘department’ );
What I can’t figure out is how do I filter by 2 criteria – for example active and department?
What if I want to exclude certain members for example?
I’m also not sure how to sort these items. Right now it’s coming up as I want because the posts have all been dragged into my desired order. However, I am trying to minimize the amount of maintenance required as people are hired, terminated, promoted, or switch departments.
[ad_2]
>What I can’t figure out is how do I filter by 2 criteria – for example active and department?
[https://wordpress.stackexchange.com/a/409523]) – example here using tax_query – but the idea is the same for meta_query e.g.
$meta_query = array(
‘relation’ => ‘AND’,
array( ‘key’ => ‘department’, ‘value’ => ‘fiscal’),
array( ‘key’ => ‘active’, ‘value’ => ‘active’)
);
$query->set(‘meta_query’, $meta_query);