Questions tagged [meta-query]
Refers to a mechanism built into the WP_Query class for querying the database for post data, including pages and CPTs, based upon post meta data.
1,059 questions
0
votes
1
answer
12
views
Query 'orderby' when there are multiple values for the same meta_key
I'm trying to create a custom sortable admin column for a custom post type that contains a "repeater" field. The plugin I'm using for this field saves the values as separate entries in the ...
0
votes
1
answer
16
views
How to order product for key exists?
I'm trying to order the woocommerce products by the existence of a specific meta key, in particular: I need to display first the products that have this meta key, and the all the products that doesn't ...
0
votes
0
answers
22
views
Select posts using @wordpress/data with query based on custom field always returns all records
I am trying to select records that have a custom field value that matches a search condition but the result is always all records
To register the custom field for my post type I am using the following ...
0
votes
0
answers
19
views
Inequality based on today's date and SCF date not working
I have the following custom query and I would like to exclude any posts whose SCF dates are in the past. I have checked that the custom date of the post is returned in the Ymd format and it is. I can ...
1
vote
0
answers
40
views
Display posts in correct month order using single date custom field
I have developed a WP Query that displays posts from a custom post type and taxonomy, and orders them by date using a custom field.
The posts have a single Advanced Custom Fields date picker field ...
1
vote
1
answer
33
views
Using Nested Blocks With Custom Query Block
I'm experimenting with a custom query block for the fun of it. I would like to leverage the components that come with core query inner blocks (title, excerpt, featured image, etc..). For this test, I'...
0
votes
0
answers
25
views
Problem making a WPQuery with ACF boolean
Well after hours and hours I find myself finally here, I am building a gutenberg block that is getting a custom post type data and making little cards with it, it works very well if I want to get:
All ...
1
vote
1
answer
23
views
Sort posts based on an acf field called fecha value return longtext '20240517'
I have a field called acf date for certain posts and I need to sort these posts by this field, but the value comes in this format: '20240517' and I need to sort the posts in ascending order with this ...
0
votes
1
answer
104
views
Meta query with ACF relationship field
I have a CPT that contains Employee Bio information. Because we don't want to create accounts in wp_users for everyone, this data is implemented as a "regular" CPT living in wp_posts. Let's ...
0
votes
0
answers
14
views
Meta query sort order is lexigraphical instead of numeric
I'm working with lumber and have custom post types with ACF fields for height and width in decimals.
I'm trying to sort my posts first by height and then by width. I am using the following code:
// ...
0
votes
0
answers
14
views
WP_User_Query get_count issue with meta_query relation
I'm using WP_User_Query to count the total number of users based on specific criteria. Generally, the following code works well:
$args = [/* your arguments here */];
$query = new WP_User_Query($args);
...
0
votes
0
answers
28
views
meta_query post_date not returning results
I'm working on a query where I have a few conditions, one of them involving post_date. However, whenever I try to write my query, post_date doesn't return any matches. If I comment that part out, it ...
0
votes
0
answers
32
views
Admin products page column not sorting
I import products to my woocommerce site, and all the imported products have an category called "external".
Note: All the external products have a "_ei_product" meta key with an &...
0
votes
1
answer
25
views
WP Query: orderby with one meta key, but multiple values
I'm trying to order a wp_query using one custom field. It is a radio group with three values: G, S and B (These represent Gold, Silver and Basic). I've been following the documentation and have looked ...
0
votes
2
answers
97
views
How to use meta_query to retrieve posts from multiple custom post type
I am having two custom post type "meet_our_community" and "news"
I have added a checkbox field named 'add_to_meet_our_community' from the acf to 'news' post type.
Now I when tried ...
0
votes
1
answer
32
views
WP_Query not using relation key as expected and not producing any results
I have created a custom post type service-provider with custom taxonomies region and service-category - By itself, this search works as expected BUT I want to also search on an ACF field.
I have an ...
0
votes
1
answer
42
views
Is there a way to use MySQL JSON functions in meta queries?
There are JSON functions: https://dev.mysql.com/doc/refman/8.3/en/json-search-functions.html can be used to search in complex data with MySQL. In the case of meta keys and values it is an issue that ...
2
votes
0
answers
28
views
get_meta_sql hook is not firing
WP_Query uses WP_Meta_Query. I thought this calls the WP_Meta_Query::get_sql() to define the meta query SQL which is where the get_meta_sql filter is applied.
I can't get the get_meta_sql filter to ...
0
votes
1
answer
99
views
Passing a variable containing a comma separated list of values in a meta-query
My objective here is to collect a series of values from a custom repeater field into a comma separated list so they can be used in a meta query. The first part of this is operational, and I think ...
0
votes
1
answer
37
views
Order posts base on last favorite post by user
How can I order favorite posts based on the last post favorited by a user? Currently favorite posts appear based on post date.
$favorites = get_user_favorites();
$favorites_query = new WP_Query(array(
...
1
vote
0
answers
27
views
How can meta values from another site in a multisite be used in a custom query?
Currently, I am using the pre_get_posts action to create a custom query, ordering the posts based on a meta value like so:
add_action( 'pre_get_posts', 'my_custom_order' );
function my_custom_order( $...
0
votes
2
answers
115
views
How to get products with the same custom attribute like ean code
I Try to get all products with the same ean code, but the query doesn't work and also doesn't give any errors. What am I missing?
Here is the code I use.
if (is_product()) {
global $product;
$...
0
votes
0
answers
71
views
How can I filter records in a custom post type list in the admin based on the ACF field in the post that contains the current user?
I have a custom post type called Projects that includes an ACF field named team_members which uses User as a field type and returns a User Array. I want to filter the post type listing so that the ...
0
votes
1
answer
21
views
WP Query filtering by custom category not showing all relevant posts
Here's a question I've been trying to solve all day:
I added a WP Query that would show posts from a category that the admin chooses with a custom field. The post is a custom post type called "...
0
votes
0
answers
23
views
SQL query based on two different custom field values
I have custom post type "store" that has two different custom fields: Coutry and City. When in the single country page I list all the stores in that country.
What I would like to do is to ...
0
votes
2
answers
40
views
Multiple meta value orderby clauses in a WP_Query
Is there a way to allow for multiple meta value orderby clauses in a WP Query? I have a query which retrieves records between meta keys start/end, and orders them ascending. I’m adding a new key for ...
0
votes
1
answer
505
views
getEntityRecords/useEntityRecords: How to use CPT metadata?
I use getEntityRecords as described in the official data API tutorial (https://learn.wordpress.org/lesson/retrieving-wordpress-data-records/).
I.e.:
const [searchTerm, setSearchTerm] = useState( ''...
1
vote
1
answer
53
views
How to compare Event period with Week period using get_posts(), meta_query, relation and compare
I have many posts, each post is an event with a period .
This period can be 3 hours, 3 days, 15 days, 3 month.
I defined the start time of event with start_timestampevents and the end with ...
0
votes
1
answer
54
views
Complicated `orderby` based on text in custom field
I'm doing a user query and looking to query users that are in a certain department (custom field) -- this works. Now I'm trying to manipulate the order....I'd like to use the meta_key for position and ...
0
votes
1
answer
22
views
Query custom fields with three dates - start and end does not work
I have tried to make a reservation system for a book a table in restaurant.
But, I have a problem when I try to see/ make new reservations in an specific day - in showing me which table are free at ...
0
votes
2
answers
79
views
Meta query array - same order as specified order in value
I have a custom Wordpress query that is using an array of product IDs that exist in an ACF field(these IDs do not line up with the Wordpress post IDs). I would like for the order of the returned posts ...
1
vote
0
answers
81
views
Post Query with Meta Query no longer working
I am working on a magazine website that has a large amount of posts, submissions, etc. There is about 60K+ posts/submissions on the site right now. I don't know when it happened specifically, but ...
0
votes
1
answer
55
views
WordPress - Optimize the Meta Query for 3 meta keys at a time
Records in post_meta as follow:
post_id
key
value
1
price
10
1
price_to
5000
2
price_type
'POA'
3
price
12000
3
price_to
17000
4
price
1200
4
price_to
8000
5
price_type
'POA'
Code Snippet
&...
0
votes
0
answers
305
views
WP Query returning all posts when Meta_query is null
I have a situation here where we have two input fields, one is searching by custom meta fields (ACF) the second is a free search.
the client is asking me for results to display in an OR fashion.
If ...
1
vote
1
answer
103
views
Order custom post type by posts with most likes first
I am using the Like/Dislike plugin on a custom post type and am trying to order the display of posts by the most like count down to those that have no likes (and no set meta value in the posts meta ...
0
votes
0
answers
59
views
Display only posts with thumbnails
I'm trying to have a WP theme display only posts that have a thumbnail.
Below is the code of the file that shows some posts on the frontpage. I've already tried to change the code (please see the //...
0
votes
0
answers
192
views
Use value from meta key array for use in WP_Query
I have a CPT named 'Physicians', and have assigned an ACF field named locations to that post type — locations is an ACF post object field.
I also have a 'Locations' CPT and intend on displaying the ...
2
votes
1
answer
916
views
Custom query with custom filtering returning incorrect results
I've setup a custom query, I'm using Woocommerce and I am aware that it has it's own functionality, but have been tasked with creating custom filtering with a traditionally paged product/category ...
0
votes
1
answer
559
views
Problem with custom WordPress Rest API search route with query parameters
I'm building a search component in a ReactJs application sitting on top of a headless WordPress CMS and I've run into a problem which I can't seem to fix. It might be that I'm doing something ...
0
votes
0
answers
28
views
Meta_query question
There is a problem with compiling a request to display posts by meta_query.
Need to output posts whose title contains a substring.
Can't figure out where I'm wrong. Can someone help?
$inputValue = '...
2
votes
1
answer
187
views
Custom taxonomy with custom meta value is not sorting correctly (query returns the same value for orderby regardless of sort column click)
I have a weight class category, where I want to sort by weight so Heavyweight shows at the top, and Flyweight at the bottom. I have created a custom meta field called 'weight' where I store an int ...
0
votes
1
answer
37
views
Showing Counts on Comment List with Custom Query
I have added a meta value to the comments-meta table that adds a custom 'status' type code (called 'redacted') to a comment. I have used the following code to display a link to display only those ...
0
votes
1
answer
58
views
Use meta query only 3 or more results?
Using elementor I'm currently running the following:
add_action( 'elementor/query/model-query', function( $query ) {
$meta_query = $query->get( 'meta_query' );
$meta_query = array();
...
0
votes
1
answer
85
views
Merge meta_value's into a single row and then loop
I have a query where I want to merge values from the wp_users and wp_meta tables, including the meta values of a given meta key. This key could have multiple entries.
SELECT
u.id,
(select ...
0
votes
0
answers
134
views
Is it possible to customize meta query in this way?
I have a custom post type called promotions.
In each promotions' post meta, I store two fields, promo_start_date and promo_end_date.
The idea is to only show promotions on the frontend, where the
...
0
votes
1
answer
78
views
Shortcode attributes from meta query variables
In the following shortcode, I have two variable, $today and $tommorrow that are being called inside a custom meta query.
function display_prognostika_home_loop($atts, $content = null) {
// Get all ...
1
vote
1
answer
300
views
get_post_meta bringing back results, but $wpdb->postmeta doesn't
I'm trying to discover if post exists, based on post_title & metadata post_language.
Until now, the best way I discovered, was using methods appearing in posts like this one, this one, or this one....
0
votes
1
answer
156
views
WP_Query by meta key not returning any posts
I have set a custom meta key 'post_views' on a custom post type which I can see in my database has been added for 3 posts.
I now need to retrieve the ID's for these posts by searching for all posts ...
0
votes
0
answers
78
views
Hook to filter based on form value and insert term
I am working on a form where I am trying to insert a term to a taxonomy. So I have a drop down select with a list of my taxonomies and then the next field is a simple text field where the users will ...
0
votes
0
answers
184
views
Wordpress Meta Query very slow with NOT EXISTS comparison
I need to get posts which do not have a certain meta key.
function handle_empty_date_paid($query, $query_vars)
{
if (!empty($query_vars['not_paid'])) {
$query['meta_query'][...