I recently added taxonomies to my user settings page using this tutorial and the ACF taxonomy field, although I didn't see anyway to query on taxonomy based on this documentation I figured I would just try it using these arguments:
$args = [
'role' => 'gebruiker',
'number' => $limit,
'offset' => $offset,
'tax_query' => array(
array(
'taxonomy' => 'category_level',
'terms' => 36,
'field' => 'term_id',
)
)
]
But it doesn't work and just returns every user that matches the other arguments, so I was wondering if it is at all possible to query based on taxonomy and if so, how do I do this? :)
Altough I never did figure out the issue, I did find a solution that works for me ;)
$args = [
'key' => 'specialty',
'value' => '47',
'compare' => '='
]
WP_User_Query($args);
Returns only 2 users, which is what I expected since only 2 users have the specialty taxonomy set to a term with the ID 47! But as soon as I wrapped this query in a meta_query tag (see below) so I could add more parameters such as the users role. It would return every user matching the role query.
$args = [
'meta_query' => [
'key' => 'specialty',
'value' => '47',
'compare' => '='
]
]
WP_User_Query($args);
So, what I ended up doing was writing a custom MySQL query to fetch the ids of the users which had to custom taxonomy set, afterwards I could use the normal WP_Query with the role parameter and I just added the include parameter with the previously found IDs
Related
I'd like to create a WP query that allows me to look for posts with particular tag slugs or pages with particular ids.
Here's what I've come up with:
$query = new WP_Query([
'post_type' => array('any'),
'posts_per_page' => $posts_per_page,
'tag_slug__in' => $tags_as_array,
'post__in' => $post_ids_as_array
]);
But this doesn't work. It appears to work only when I remove the "tag_slug__in" key or the "post__in" key.
Is there a way to do this using WP_Query? I was hoping to not have to create a custom SQL statement.
tag_slug__in
only works for a blog post, not for custom post types or others. There is a reason that your query is not working correctly.
You need the tax query for custom post types or others.
Please follow this:
The WordPress Query class
$query = new WP_Query( [
'post_type' => [ 'any' ],
'posts_per_page' => $posts_per_page,
'post__in' => $post_ids_as_array,
'tax_query' => [
[
'taxonomy' => 'your-taxonomy-name',
'field' => 'slug',
'terms' => $tags_as_array,
'operator' => 'IN'
],
]
] );
I am using Search and Filter Pro plugin to fetch search result based on various post meta and taxonomies, I am not completely using the plugin's entire interface only its custom query approach:
https://searchandfilter.com/documentation/search-results/custom/
I attached search and filter to the WP_Query and that part looks like this:
$premium_args = array(
"s" => $_GET['q'],
'posts_per_page' => 20,
'paged' => 1,
'post_type' => array( 'contactdirectory'),
'orderby' => 'title',
'order' => 'ASC',
);
$premium_args['search_filter_id'] = 166;
And in the plugin I have set the 'Field relationships:' to AND for that search and filter instance.
However it's not working as expected, here's an example:
https://indiadairy.com/?s=&p_type=cd&_sft_Industries=equipment-manufacturers&_sfm_state=Maharashtra
_sft_Industries=equipment-manufacturers is there, and then _sfm_state=Maharashtra but in the search results you can see there are results which has a different value for Industries taxonomy like Dairy Plants and 'NGO' whereas it should only return results that satisfies the value passed to both parameters.
I tried adding
'meta_query' => array(
'relation' => 'AND',
)
It doesn't work, which I expected, as it's for meta queries passed manually in the WP_Query arguments.
I have two post types:
Venues
Reviews
The Venues Post Type contains the following ACF custom fields:
Region
Sub Region
The Reviews Post Type contains one ACF custom field:
Venue (which is Post Object - Select Field)
I need to display all Reviews who's Venue is in a specific region and/or Sub Region.
Is this something that can be accomplished using WP_Query? Or do I need to do a fancy database call?
This is what I thought would initially work but it seems that you can not get the custom field of a post object as a meta_query..
$args = array(
'post_type' => 'review',
'posts_per_page' => 18,
'paged' => $paged,
'meta_key' => 'venue',
'meta_query' => array(
array(
'key' => 'region',
'value' => 'napa-valley'
)
)
);
I think you need 2 loops here, first loop through the venues using region meta query (you could just use get_posts() or get_pages() instead of WP_Query too)
e.g.
'meta_query' => array(
array(
'key' => 'region',
'value' => 'napa-valley'
)
)
Then you can push the IDs of the venues in specific regions into an array
array_push($venue_ids, $post->ID);
Then you can use the $venue_ids array in your second loop which would loop through the review using a meta query to match the venues from your first loop ids to the post object ids selected in the review page.
'meta_query' => array(
array(
'key' => 'venue',
'value' => $venue_ids
)
)
Let me know if this is helpful and if you think this will work for you and I can offer further assistance if I haven't explained correctly or you need help.
This question seems to be unanswered on the internet, perhaps because it is not possible. I simply want to query all posts where the repeater field 'has rows'. i.e.
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'repeater_field',
'value' => '',
'compare' => '=!'
)
);
I know an alternative is to run if statements inside the loop to filter them out, but that messes with some other logic which is based off of the number of posts in the query.
Does anyone have any thoughts on this?
Let's consider you have a Repeater Field labeled My Repeater Field and this repeater contains at least one field labeled A Field on the Repeater.
Assuming you have the default wp_ table prefix, you need to look at the DB's wp_postmeta table to notice that values for this field on the repeater are stored with the meta_key:
NAME_OF_THE_REPEATER_index_NAME_OF_FIELD_ON_THE_REPEATER
So, in our case, if a post has 3 rows on the repeater field, its values will be stored as:
my_repeater_field_0_a_field_on_the_repeater
my_repeater_field_1_a_field_on_the_repeater
my_repeater_field_2_a_field_on_the_repeater
Knowing this, if you want to query all posts having at least ONE row on the repeater, you could do:
$meta_query = [
[
'key' => 'my_repeater_field_0_a_field_on_the_repeater',
'compare' => 'EXISTS',
]
];
$args = [
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query,
'post_type' => 'post',
'post_status' => 'publish',
];
$posts_array = get_posts( $args );
Note: As stated on WP Docs, you can only use the EXISTS comparison on WP >= 3.5 and you don't need to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up. I'm also assuming you are using PHP >= 5.4 so you can use short array syntax. If not, just replace [] for array().
You can query the wordpress database using the $wpdb object. ACF fields are saved in prod_postmeta on the database so that is where you will run your query. Your meta_value will be the key of your repeater field, so make sure you replace that in the query below. All keys for any ACF field with start out with field_ and then random characters/digits will follow like seen below. Then once you have the post id, you can run get_post() on those post ids. Let me know if you need anything else or have questions.
global $wpdb;
$results = $wpdb->get_results("SELECT post_id from prod_postmeta WHERE meta_value = 'field_534eeaaa74199'");
$echo $results;
This works. I have test it. Only by the "Welcome World" post it doesn't work.
$args = array(
'post_type'=> 'post',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'repeater_field',
'value' => '0',
'compare' => '!='
)
));
$the_query = new WP_Query( $args );
The "repeater_field" is the name of the field, not the field_key. Here is the count of the rows.
I'm working with AdvancedCustomFields and Taxonomies, and I don't find, in the documentation, something that allows me to filter the Taxonomies by the values stored on the wp_options table.
I found that here.
And, it could by something like this, but with Taxonomies:
I have a taxonomy called "Person", and I have many fields. For example, I would like to filter by sex and country.
Is there any function that allows me to do this? Or should I work with WP-Query?
Thanks in advance
Once you have registered the taxonomy with your post you may try with tax_query Query inside the your query_post
query_posts( array(
'post_type' => 'your post type',
'paged' => $paged,
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'person', //or tag or custom taxonomy
'field' => 'id'
)
)
) );