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.
Related
UPDATE: I mean to search user in the Admin User Dashboard at /wp-admin/users.php
Is there a way to search users using 'first_name' and 'last_name'?
I found this plugin but not working at all: https://wordpress.org/plugins/full-name-search-in-wp-admin/
I also found this function, but neither is working in my site. Do I need to call the function with a hook/action?:
function search_users($search_term){
global $wpdb;
$the_users = $wpdb->get_results("SELECT DISTINCT $wpdb->users.* FROM $wpdb->users INNER JOIN $wpdb->usermeta um1 ON um1.user_id = $wpdb->users.ID JOIN $wpdb->usermeta um2 ON um2.user_id = $wpdb->users.ID WHERE (um1.meta_key = 'uRPhICRS_capabilities' AND um1.meta_value LIKE '%agent%') AND ((um2.meta_key = 'first_name' OR um2.meta_key = 'last_name') AND um2.meta_value LIKE '%".$search_term."%');");
if(!empty($the_users)){
return $the_users;
}
else{
return false;
}
}
Two Ways as follow:
1) If You want to check from the string match in either firstname or lastname, following will be work:
$users_query = new WP_User_Query(
array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $str,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $str,
'compare' => 'LIKE'
)
)
)
);
$users = $users_query->get_results();
2) If you want to check the user from username, user_nicename, user_email, firstname, or lastname use following code:
//search from the user table
$users_query_table = new WP_User_Query(
array(
'search' => "*{$str}*",
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
),
) );
$users_via_table = $users_query_table->get_results();
//search from the usermeta
$users_query_meta = new WP_User_Query(
array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $str,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $str,
'compare' => 'LIKE'
)
)
)
);
$users_via_meta = $users_query_meta->get_results();
// Merge both result..
$combined_users = array_merge( $users_via_table, $users_via_meta );
// Get unique user
$users = array_unique( $combined_users, SORT_REGULAR );
You can use WP_User_Query.
Example:
// The search term
$search_term = 'Ross';
// WP_User_Query arguments
$args = array (
'role' => 'reporter',
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*'.esc_attr( $search_term ).'*',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'description',
'value' => $search_term ,
'compare' => 'LIKE'
)
)
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);
https://developer.wordpress.org/reference/classes/wp_user_query/
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".
I would like to retrieve all those posts whose author (post table field) is a given one OR those which has a given meta value (postmeda table field).
If "author" was a meta value, I know I could use a meta_query to achieve it. The thing here is that it is not... so I think I cannot use the "author" field within a meta_query and use the "relation" key.
I'm looking for something like:
$args = array(
'post_type' => array('post'),
'orderby' => 'ASC',
'order' => 'date',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'field' => 'author',
'value' => $author_id,
'compare' => '==',
),
array(
'key' => '_meta_field_name',
'compare' => 'NOT EXISTS',
),
),
array(
'relation' => 'AND',
array(
'key' => '_meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => '_meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
),
);
$data = new WP_Query( $args );
Any suggestion on how to achieve that using WP_Query?
Thanks!
You might like to try an approach like this one instead. The idea is to query the two conditions you want to search for, then merge the two queries into one finished product.
//Get posts with the author you're looking for
$args1 = array(
'author_name' => 'testuser', //or 'author' => $author_id or something else
);
$data1 = get_posts( $args1 );
//Get posts with the meta data you're looking for
$args2 = array(
'meta_query' => array(
array(
'key' => 'meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => 'meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
);
$data2 = get_posts( $args2 );
//Merge both arrays
$allData = array_merge( $data1, $data2 );
//Get just the IDs of all the posts found, while also dropping any duplicates
$postIDs = array_unique( wp_list_pluck( $allData, 'ID' ) );
//Do a new query with these IDs to get a properly sorted array of post objects
$args3 = array(
'post__in' => $postIDs,
'order' => 'ASC',
'orderby' => 'date',
);
$finalAnswer = get_posts( $args3 ); //This is your array of post objects. Ta-Da!
i'm facing wordpress meta_query problem, from this simple query i am not able to output values :
(
[post_type] => offres
[meta_query] => Array
(
[0] => Array
(
[key] => topics
[value] => Array
(
[0] => PADINE
[1] => ALKEKENGE
)
[compare] => IN
)
)
)
This is the php code :
$args = array(
'post_type' => 'offres',
'meta_query' => array(
array(
'key' => 'topics',
'value' => array('PADINE','ALKEKENGE'),
'compare' => 'IN'
)
)
);
On my wordpress admin i have a post assigned with 'topics' meta key and 'PADINE' as value, i wonder why is it not working ?
Thank you for your helps.
Sometimes, it is hard to get the meta value due to the serialize structure of the data.
$args = array(
'post_type' => 'offres',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'topics',
'value' => 'PADINE',
'compare' => 'LIKE',
),
array(
'key' => 'topics',
'value' => 'ALKEKENGE',
'compare' => 'LIKE',
)
)
);
Hope it works,
Thanks
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