<?php
// Query to retrieve users where the ACF field is not empty
$user_query = new WP_User_Query(array(
'meta_query' => array(
array(
'key' => 'designations', // Replace with your ACF field name
'compare' => 'EXISTS', // Check if the field exists
),
),
));
// Check if users were found
if (!empty($user_query->results)) {
$all_chosen_values = array(); // Array to store all chosen values
// Loop through the users
foreach ($user_query->results as $user) {
// Get the values of the ACF field for the user (assuming it's an array)
$values = get_user_meta($user->ID, 'designations', true); // Replace with your ACF field name
// Check if values is an array
if (is_array($values)) {
// Merge values into the all_chosen_values array
$all_chosen_values = array_merge($all_chosen_values, $values);
}
}
// Remove duplicates
$all_chosen_values = array_unique($all_chosen_values);
// Get the field object
$field = get_field_object('field_664517d942b6e'); // Replace with ACF field name
if ($field) {
// Output the chosen values as checkboxes
foreach ($all_chosen_values as $value) {
if (array_key_exists($value, $field['choices'])) {
echo '<div class="form-checkbox-option"><input type="checkbox" id="' . esc_attr($value) . '" name="designations" value="' . esc_attr($value) . '">';
echo '<label for="' . esc_attr($value) . '">' . esc_html($field['choices'][$value]) . '</label></div>';
}
}
}
}
?>
]]> //Get Search and filter values from Ajax
$mediator_search = $_POST['mediator_search'];
$designations = $_POST['designations'];
$region = $_POST['region'];
$languages = $_POST['languages'];
$hourly_rates = $_POST['hourly_rate'];
$associate = $_POST['associate'];
$sliding_scale = $_POST['sliding_scale'];
$indigenous_mediator = $_POST['indigenous_mediator'];
//Dynamically create variables from form values that contain arrays - each created variable will be formatted 'designation_[number]'
$designation_count = 1;
foreach ($designations as $designation) {
$des = 'designation_'.$designation_count;
$$des = $designation;
$designation_count++;
}
$args = array(
'role' => 'mediator',
'number' => $per_page,
'paged' => $paged,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
// Values in this meta field are an array
'key' => 'designations',
'value' => array ( $designation_1 ),
'compare' => 'IN'
),
array(
'key' => 'region',
'value' => $region,
'compare' => 'LIKE'
),
array(
'key' => 'associate',
'value' => $associate,
'compare' => '='
),
array(
'key' => 'sliding_scale',
'value' => $sliding_scale,
'compare' => '='
),
// The value in this meta field is either a 1, or nothing
array(
'key' => 'indigenous_mediator',
'value' => $indigenous_mediator,
'compare' => '='
),
// Trying to search keywords from a search form field here
array(
'key' => 'first_name',
'value' => $mediator_search,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $mediator_search,
'compare' => 'LIKE'
),
array(
'key' => 'region',
'value' => $mediator_search,
'compare' => 'LIKE'
),
)
);
Thanks in advance, and please let me know if any other details are needed!
]]>The p2p_id
in the “Connected Users” admin meta box is empty.
I think this is related the the following changes since WordPress 6.1:
Also see the following code:
I think Posts 2 Posts joins in some extra columns (wp_p2p.*
), but that data is no longer available due to the additional caching mechanisms introduced since WordPress 6.1.
SELECT SQL_CALC_FOUND_ROWS wp_users.ID, wp_p2p.*
FROM wp_users
INNER JOIN wp_p2p
I was able to get around the caching mechanism somewhat by defining specific fields in the query:
<?php
add_action(
'pre_get_users',
function( $query ) {
if ( 'admin_box' === $query->get( 'p2p:context' ) ) {
$query->set( 'fields', [ 'id', 'display_name' ] );
}
}
);
That does solve the problem, but I have no idea if this has any further side effects. Can someone properly fix this problem and launch an update?
Defining the fields seems to work through the following lines of code:
The following topics are probably related:
]]>WP_User_Query
:
$args2 = array(
'meta_query' => array(
'gender' => array(
'key' => 'gender',
'value' => 'female',
'compare' => '='
)
),
'number' => 10
);
$user_query = new WP_User_Query( $args2 );
The query is returning unfiltered results, basically ignoring the meta_query
. It works fine if I run it from functions.php
or from a template file, but not from ajax
. Any idea why?
This is the?request
?string when it works:
SELECT SQL_CALC_FOUND_ROWS wp_users.ID
FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id
)
WHERE 1=1 AND (
( wp_usermeta.meta_key = 'gender' AND wp_usermeta.meta_value = 'female' )
)
ORDER BY user_login ASC
LIMIT 0, 10
And this is the one from the ajax function:
SELECT SQL_CALC_FOUND_ROWS wp_users.ID
FROM wp_users
WHERE 1=1
ORDER BY user_login ASC
LIMIT 0, 10
]]>Name Number of post ASC/DESC
ueser x 50
user y 40
user u 20
Here I have an example using WP_Query, I need to learn interaction database like sql to achieve what I want
<?php
$args = array (
'post_type' => 'post',
'posts_per_page' => '16',
);
$the_query = new WP_Query ($args);
if ($the_query-> have_posts()) {
$authorArgs = array(
'orderby' => 'ID',
'posts_per_page' => 1,
'has_published_posts' => array('post'),
);
$authors = get_users();
$authors = get_users( array( 'user_registered' => 'blankwordpress' ) );
echo '<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr> ';
foreach ( $authors as $author ) {
$posts = count_user_posts($author->ID, 'post');
echo '<tr><td>' . esc_html( $author->first_name) .' </td><td>' . esc_html( $author->last_name) . '</td><td> ' . $posts . '</td></tr>';
echo '</table>';
echo '<ul>';
while ($the_query-> have_posts()) {
$the_query-> the_post();
echo '<li>'. get_the_title(). '</li>';
echo '<li>'. get_the_author(). '</li>';
}
echo '</ul>';
} else {
// no posts found
}
?>
]]>The database structure is as follows (using Ultimate Member plugin):
User is stored in wpma_users table
User metadata in table wpma_usermeta contains user meta named mygroups, containing a value a:1:{i:0;s:3:”155″;}
The 155 value is the ID of a term
I already tried this, with $groupid=155:
$args = array(
'fields' => 'all_with_meta',
'tax_query' => array(
array(
'taxonomy' => 'mygroups',
'field' => 'term_id',
'terms' => $groupid,
),
),
);
$user_query = new WP_User_Query($args);`
When I var_dump get_terms(array(‘include’ => $groupid)), an actual term is printed:
array(1){
[
0
]=>object(WP_Term)#27633(10){
[
"term_id"
]=>int(155)[
"name"
]=>string(8)"Example Group"[
"slug"
]=>string(8)"example_group"[
"term_group"
]=>int(0)[
"term_taxonomy_id"
]=>int(155)[
"taxonomy"
]=>string(11)"um_user_tag"[
"description"
]=>string(0)""[
"parent"
]=>int(149)[
"count"
]=>int(1)[
"filter"
]=>string(3)"raw"
}
}
Another way I tried, is using the meta_query instead of the tax_query:
'meta_query' => array(
array(
'key' => 'mygroups',
'value' => $groupid,
'compare' => 'LIKE',
)
),
This actually filters the query, but when I use $groupid=1, it filters where groupid contains 1, so also 10, 11, 100 etc.
How do I find users with a specific value that’s listed in the mygroups user meta field using WP_User_Query?
]]>I tried to create a search on a custom page that contains a list of users, but I can’t make it work.
I created a custom page template to mount my page, this page list some data of registered users who have the “shopkeeper” role using a “WP_User_Query” query and also a “WC_Customer” query (because some user data comes of WP_User_Query and some other data comes of WC_Customer) and I would like to be able to search the results of the querys on this page, with a text entry (which I called “searchInput”).
Every time I do a search for the data that comes from the query WC_Customer (E.g.: billing_city), it does not give me results. I think something’s missing.
This is my code:
<?php
/* Template Name: List Users */
...
<form id="searchForm" method="post" >
<input class="formControl inputSearch" placeholder="Nome da Loja, Cidade ou Estado" name="searchInput">
<button type="submit" class="btn btn-primary">Pesquisar</button>
</form>
<ul id="userListContainer">
<?php
$searchInput = $_POST['searchInput'];
// WP_User_Query arguments
$args = array(
'meta_key' => 'first_name',
'role' => 'shopkeeper',
'search' => $searchInput,
'search_columns' => array(
'first_name',
'last_name',
'billing_city',
'billing_state'
),
'order' => 'ASC',
'orderby' => 'meta_value',
'fields' => 'all_with_meta',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $searchInput,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $searchInput,
'compare' => 'LIKE'
)
)
);
// The User Query
$users = new WP_User_Query($args);
// User Loop
if (!empty($users->results)) {
foreach ($users->results as $user) {
// Get an instance of the WC_Customer Object from the user ID
$customer = new WC_Customer($user->ID);
$username = $customer->get_username(); // Get username
$user_email = $customer->get_email(); // Get account email
$first_name = $customer->get_first_name();
$last_name = $customer->get_last_name();
$display_name = $customer->get_display_name();
// Customer billing information details (from account)
$billing_first_name = $customer->get_billing_first_name();
$billing_last_name = $customer->get_billing_last_name();
$billing_company = $customer->get_billing_company();
$billing_address_1 = $customer->get_billing_address_1();
$billing_address_2 = $customer->get_billing_address_2();
$billing_city = $customer->get_billing_city();
$billing_state = $customer->get_billing_state();
$billing_postcode = $customer->get_billing_postcode();
$billing_country = $customer->get_billing_country();
echo
'<li>' .
'<a href="' . esc_url(get_author_posts_url($user->ID)) . '"target="_blank">' .
'<div id="userImage">' . get_avatar($user->ID) . '</div>' .
'<div id="userInfo">' . esc_attr($user->first_name) . ' ' . esc_attr($user->last_name) . '<br>' .
'CEP: ' . esc_attr($billing_postcode) . ' — ' . esc_attr($billing_city) . ', ' . esc_attr($billing_state) . '<br>' .
'</a>' .
'<a href="' . esc_attr($user->user_url) . '">' . esc_attr($user->user_url) . '</a>' .
'</div>' .
'</li>';
}
} else {
echo 'Nenhum usuário encontrado.';
}
?>
</ul>
</main><!-- #main -->
</div><!-- #primary -->
<?php
//do_action( 'ekommart_sidebar' );
get_footer();
?>
Any help is welcome! Thanks in advance!!
]]>My site is running WordPress 5.9.3.
I have to admit I don’t fully understand differences between user_query and meta_query, but have been following examples from various sources online, including here.
This produces the first result correctly, selecting values in the range of 1970 to 1979 but no sorting tried:
$args = array(
'meta_key' => 'class_of',
'meta_value' => $decade,
'meta_type' => 'NUMERIC',
'meta_compare' => 'BETWEEN'
);
$user_query = new WP_User_Query ($args);
$decade is 1970, 1979. That seems to work OK. I’m seeing meta_value = 1970, 1979 if I echo it out.
I have tried 30-40 changes over the last few days and read the doc and numerous searches to try to solve this, but as soon as I add any nested arrays, the query will retrieve all users. For instance this, which is presently at the above link (select 1970s) and patterned after something I saw in this forum that explained how to orderby two meta_values:
$args = array(
'meta_query' => array(
'relation' => 'AND',
'decade_clause' => array(
'meta_key' => 'class_of',
'meta_value' => $decade,
'meta_type' => 'NUMERIC',
'meta_compare' => 'BETWEEN'
),
'lname_clause' => array(
'meta_key' => 'last_name',
'meta_value' => ''
),
),
'orderby' => array(
'decade_clause' => 'ASC',
'lname_clause' => 'ASC'
),
);
$user_query = new WP_User_Query ($args);
It seems like this should work, at least to select the correct years. There should only be 3 rows retrieved, as only 3 users have a ‘class_of’ meta_value in the 1970s at the moment. Most current users on my site registered before the latest forms were created. So they show up as empty rows when all users are retrieved.
I just have not been able to figure out where I’m going wrong. If anyone could point me in the right direction –
Thanks – Brian
]]>SELECT SQL_CALC_FOUND_ROWS wp_users.*
FROM wp_users
INNER JOIN wp_usermeta
ON ( wp_users.ID = wp_usermeta.user_id )
WHERE 1=1
AND ( ( ( wp_usermeta.meta_key = 'wp_capabilities'
AND wp_usermeta.meta_value LIKE '%\"Administrator\"%' ) ) )
ORDER BY user_login ASC
This is in wp_user_query
My current site have around 100k+ Orders. It isnt that slow, but I notice wordpres keep calling this SQL Query again and again. Is there any possibility I can put this in transient cache? To speed up the wp-admin? Thank you
Essentially, I’m storing a list of users (contacts) in an array, and, if that is set, I want to search through those users, only.
I thought this would work:
function searchfilter($query) {
$my_contacts = array(11, 19); // setting just to check it's working
$search_type = get_query_var('search_type');
if ($query->is_main_query() && $search_type == 'contacts' ) {
$query->set('include', $my_contacts);
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
But it doesn’t limit the query at all.
Appreciate any help!
]]>