I'm looking to query ACF subfields from a group within a group.
Here's my (stripped) code to show where I'm trying to target:
acf_add_local_field_group(array(
'key' => 'events_infos',
'fields' => array(
'key' => 'general_infos',
'type' => 'group',
'sub_fields' => array(
array(
'key' => 'dates',
'sub_fields' => array(
array(
'key' => 'start', // This is my sort key
...
)
)
)
)
));
I want to query the upcoming events chronologically so I would need to access; how would I format my key in the following meta_query?
get_posts(array(
'meta_query' => array(
array(
'key' => **events_infos[general_infos][dates][start]**,
'order' => 'ASC'
)
)
)
As indicated by #Stender in his comment, the answer can be found in the documentation under the heading 4. Sub custom field values".
Related
I'm running a query on users and want to include only users who have meta data (page_history) with a specific value. The problem I'm having is that the meta data is an object, and I can't seem to get the query to check the value of the object:
$role = isset($_GET['role'] && $_GET['role'] !== '');
$args = array(
'role' => $role ?? '',
'orderby' => 'user_nicename',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
$search_term[0] ? array(
'key' => 'first_name',
'value' => $search_term[0],
'compare' => 'LIKE'
) : '',
$search_term[1] ? array(
'key' => 'last_name',
'value' => $search_term[1],
'compare' => 'LIKE'
) : '',
array(
'key' => 'page_history',
'value' => 'custom_post_type',
'compare' => 'EXISTS'
)
),
);
$users = get_users($args);
foreach ($users as $user) {
// Do stuff
}
Here is my meta object:
Array
(
[date] => 1655387457
[post-type] => custom_post_type
[page] => Page title here
)
So I just want to get users if they have the meta data page_history set as 'custom_post_type'.
Thank you
This meta object will be serialized before being stored in the database column.
The post-type field after serialization will be like that in DB s:9:"post-type";s:16:"custom_post_type";
You can use LIKE compare to filter it.
array(
'key' => 'page_history',
'value' => 's:9:"post-type";s:16:"custom_post_type";',
'compare' => 'LIKE'
)
You will need to change the number 16 to the custom post type name count.
I am trying to fetch the result if keyword, category and meta value is not empty. I mean to say that if the keyword is test and city is Mumbai and category is pet then show existing results that come in these parameters. Now I am getting all the results which have in other categories too.I have two inputs , one for keyword, second for City, zip code and third one for categories drop down.
Any suggestions would be greatly appreciated.
Expected result should be keyword,city under selected category.
$arg = array(
'post_type' => 'post',
'posts_per_page' => 10,
's' => $keyword,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' =>$cats
)
),
'meta_query' => array(
array( 'key' => 'city', 'value' => $query_city, 'compare' => 'LIKE' ),
array( 'key' => 'country', 'value' => $query_city, 'compare' => 'LIKE' ),
array( 'key' => 'postalcode', 'value' => $query_city, 'compare' => 'LIKE' ),
'relation' => 'OR'),
);
$query = new WP_Query( $arg );
If I understand you right, using 'relation' => 'AND' instead 'relation' => 'OR' should solve your issue.
I have a select field created with custom field . Field contains a list of numbers from 1 to 10 .
How could do to make the post an ORDER BY query this select?
"orden" is the field select type
Maybe the problem is because the field select is a string?
$args = array(
'post_type' => 'directivo',
'orderby' => 'orden',
'order' => 'ASC',
'showposts' => -1,
'meta_query' => array(
array(
'key' => 'principal_o_suplente',
'value' => 'principal',
'compare' => '=='
),
array(
'key' => 'empresarios_o_',
'value' => 'gobiernos',
'compare' => '=='
)
)
);
I am trying to pull custom post type with field value set as 'No' or not set (older posts for which this custom field is not set). The code i am using for query args is:
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'com', 'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ct_Featured_C_radio_3292',
'value' => 'No',
'compare' => 'LIKE'
),
array(
'key' => 'ct_Featured_C_radio_3292',
'value' => array(''),
'compare' => 'LIKE'
)
)));
But this meta query only pulls posts with value set as 'No' and not with posts with empty values. Please advice on how to write the meta query so all posts with No and empty values can be pulled.
"NOT EXISTS" checks will include meta_keys that don't exist.
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'com', 'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ct_Featured_C_radio_3292',
'value' => 'No',
'compare' => 'LIKE'
),
array(
'key' => 'ct_Featured_C_radio_3292',
'compare' => 'NOT EXISTS'
)
)));
Try This
http://codex.wordpress.org/Class_Reference/WP_Meta_Query
This is related to meta Query
Display posts with 'Product' type ordered by 'Price' custom field:
$query = new WP_Query(
array ( 'post_type' => 'product',
'orderby' => 'meta_value',
'meta_key' => 'price' )
);
Which code should I use if also want to order by 'Size'?
Another example on which I need multiple sort on custom fields:
Display posts with 'Event' type ordered by 'Start_Hour' and then by 'Start_Minute'.
Thanks to Bainternet I found the solution:
function orderbyreplace($orderby) {
return str_replace('menu_order', 'mt1.meta_value, mt2.meta_value', $orderby);
}
and...
$args = array(
'post_type'=>'Events',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'Start_Hour',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'Start_Minute',
'value' => '',
'compare' => 'LIKE'
)
)
);
add_filter('posts_orderby','orderbyreplace');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','orderbyreplace');
I think this changed slightly in Wordpress 3.7.
I had to change
return str_replace('menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);
into
return str_replace('wp_posts.menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);
Thanks for the initial solution! [Edited]
Here's an example of using more than one meta_key and orderby that I believe should work:
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'price',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'size',
'value' => '',
'compare' => 'LIKE'
)
),
'orderby' => 'price size',
'order' => 'ASC'
);
$query = new WP_Query;
$resulting_obj = $query->query($params);
You'll need to play with the meta_query items a bit more, especially the 'value' parameter. Please have a good look at http://codex.wordpress.org/Class_Reference/WP_Query in the 'Custom Field Parameters' section.
You don't need any filter or hooks to sort multiple custom fields, if your one of custom field is meta_key and other one is normal column of table, than use these parameters/arguments in your query.
'meta_key' => 'KEY_NAME',
'orderby' => 'meta_value_num SECOND_COLUMN_NAME',
'order' => 'ASC' or 'order' => 'DESC'
Here the order of meta_value_num and SECOND_COLUMN_NAME matter, you may see different-2 result based on the order.