Tag Info

Hot answers tagged

11

This should get you really close. I haven't tested it, but it's nearly identical to a setup I've used a few times. /* * We start by doing a query to retrieve all users * We need a total user count so that we can calculate how many pages there are */ $count_args = array( 'role' => 'Subscriber', 'fields' => 'all_with_meta', ...


9

Well I have come up with 2 solutions. Solution 1 - foreach loop and verify each user This one is based off of @GhostToast's solution, but with updated WordPress functions //new query with default args $author_query = new WP_User_Query(); // Get the results $authors = $author_query->get_results(); if( $authors ) { foreach( $authors as $author ) ...


7

You can try this: /** * Add support for the "display_name" search column in WP_User_Query * * @see http://wordpress.stackexchange.com/a/166369/26350 */ add_filter( 'user_search_columns', function( $search_columns ) { $search_columns[] = 'display_name'; return $search_columns; } ); where we use the user_search_columns filter to modify the ...


6

Searching the main table Simply use WP_User_Query with a search argument. So if you want to search for example for a user with a keyword in his user_email or similar columns from the {$wpdb->prefix}users table, then you can do the following: $users = new WP_User_Query( array( 'search' => '*'.esc_attr( $your_search_string ).'*', ...


5

The search parameter can take an email address and can also accept wildcards: $wp_user_search = new WP_User_Query( array( 'search' => '*@mydomain.com' ) ); $users = $wp_user_search->get_results();


4

Method #1: The pre_user_query hook: There are not many filters available, but you can try the pre_user_query hook: // Add filter: add_action( 'pre_user_query', 'wpse_filter_by_reg_date' ); // Query: $query = new WP_User_Query( $args ); // Remove filter: remove_action( 'pre_user_query', 'wpse_filter_by_reg_date' ); where the filter callback is: /** ...


4

@m0r7if3r provided the correct answer. For the sake of providing a directly usable snippet to anyone else looking at this issue, here's what I ended up using. $wp_users = $wpdb->get_results(" SELECT users.*, meta.meta_value FROM $wpdb->users AS users LEFT JOIN $wpdb->usermeta AS meta ON users.ID = meta.user_id WHERE ...


4

Made something myself: Page template file: <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $paged -= 1; $limit = 20; $offset = $paged * $limit; $args = array( 'number' => $limit, 'offset' => $offset, ); // Create the WP_User_Query object global $wp_query; $wp_query = new WP_User_Query($args); // Get the results ...


4

I wonder if this works for you: add_action( 'pre_user_query', 'wpse_order_by_include_values' ) $query = new WP_User_Query( $args ); remove_action( 'pre_user_query', 'wpse_order_by_include_values' ) where function wpse_order_by_include_values( $q ) { if( isset( $q->query_vars['include'] ) ) { $values = join( ',', ...


3

WP_User objects have some magic methods which do allow you to access any custom field: foreach ( $agents as $agent ) { var_dump( $agent->bio ); var_dump( $agent->get( 'bio' ) ); } The two are equivalent. More info: http://scribu.net/wordpress/the-magic-of-wp_user.html


3

I don't see an "orderby Rand()" parameter for either get_users or WP_User_Query. There is a filter called pre_user_query that could be used but I am not sure I see the benefit of that when shuffle will randomize the array you already have. $args = array( 'fields' => 'all_with_meta', 'exclude' => array(1), ); $users = get_users( $args ); ...


2

No, it isn't possible without modifications to the p2p plugin, as indicated here: https://github.com/scribu/wp-posts-to-posts/issues/374 Relevant code is here: https://github.com/scribu/wp-posts-to-posts/blob/master/core/connection-type.php#L374


2

After a lot of trial and error I learnt that WP_User_Query takes an argument fields which can be set to 'all_with_meta'. This reduces number of queries to two for any number of users that are fetched in a single go and also provides benefits of caching. Usage would look like: $user_query = new WP_User_Query( array('include' => $post_authors, 'fields' ...


2

You can make use of WP_User_Query to retrieve a list of all user's ID's and then pass that to get_userdata to retrieve a user's registration date. You can then sort and exclude users from that according to a specific date and time. Here is my idea <?php $user_query = new WP_User_Query( array( 'fields' => 'ID', ) ); $users = ...


2

You could save the values with the separate user meta keys: x1, ..., x6, points instead of the array: achievements to simplify your WP_User_Query() usage and not having to deal with serialized arrays as user meta values. But I guess you've already considered this setup and wanted to avoid it ;-) A compromise would be to move only the points out of ...


2

According to the codex, display_name isn't supported. You could use the pre_user_query filter. function henry_display_name_find( $query ) { global $wpdb; /* you don't say where the name comes from - this assumes a $_POST field */ $display_name = $_POST['display_name']; $use_like_syntax = false; if ( $use_like_syntax = true ) { ...


2

There is a better way to do this as of Wordpress version 3.7. Use the Wordpress property meta_key to select the last name property and then orderby => meta_value with an ascending order. <?php $args = array( 'meta_key' => 'last_name', 'orderby' => 'meta_value', 'order' => 'ASC' ); $user_query = new WP_User_Query( $args ); if ( ! ...


2

Filter the string 'query' and change the ORDER BY part. See https://gist.github.com/1281778#L160 for an example.


2

This is covered in the link you reference: The search_columns attribute does not set what should be searched for in each column, but instead specifies which columns should be searched for the term set in 'search'. Specifically, you can only search for one term - but you can look in one or more columns. //Search user logins & emails for 'foo'. ...


2

You shouldn't be passing in $users["results"] directly to a foreach without checking that $users is an array, and that it has a 'results' key. What if $users is empty, or false, and a WP_Error object? In this case, it isn't an array, it's a WP_User_Query object. Casting it to an array won't fix that. Instead you should be using it like this: $user_query = ...


2

You can simply use the date_query parameter on the user registration date: $args = array ( 'role' => 'subscriber', 'date_query' => array( array( 'after' => '2010-01-13 00:00:00', 'inclusive' => true, ), ), ); $user_query = new WP_User_Query( $args ); This part of the ...


2

You are using pre_user_query according to WordPress documentation Fires after the WP_User_Query has been parsed, and before the query is executed Then you should use pre_get_users just like pre_get_posts when your arguments have some meaning to WordPress. pre_get_users Fires before the WP_User_Query has been parsed Replace your hook with ...


2

I think you should use the default class for this job - WP_User_Query. The query have a lot of possibilities, also get the display name. WP_User_Query is a class that allows querying WordPress database tables _users and _usermeta. Also it is helpful when you use a cache for the values. Also here I prefer the WordPress defaults WP_Cache (Non-Persistent ...


2

When executing the generated SQL statement in MySQL it gave me The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay To fix that I had to call $wpdb->query( 'SET OPTION SQL_BIG_SELECTS = 1' ); right before calling WP_User_Query.


2

There is no tax_query parameter for the WP_User_Query class. Since you've tagged the question with user-meta I can only assume you store the ID (or even the term name itself?) of a particular shop-category term as a user meta entry. In this case you would need something like this: // The 'WP_User_Query' arguments array $args = array( 'role' ...


1

The problem is that you can't use the value for the LIKE comparison as you've mentioned. The generated SQL will look something along the lines of: AND CAST(wp_usermeta.meta_value AS CHAR) LIKE '%B%' which means that the query is set to look for every occurrence of letter B in our case. Of course this is not what you want, what you would need is something ...


1

If you push a "search" through WP_User_query you will search both the user_login fields and the user_nicename fields. That produces a de facto inefficient OR query. You can reduce that by using a filter to force the search to only the user_nicename field. add_filter('user_search_columns','nicename_only_wpse_188846'); function ...


1

You need to use the user_search_columns filter to add the columns you want to search on. The user_search_columns filter is used to determine which user fields in the database are used when performing a search on user information. https://codex.wordpress.org/Plugin_API/Filter_Reference/user_search_columns


1

This is a rough draft of what I have so far. Please feel free to update it and improve it. First of all, we use the pre_user_action to choose the minimum number of posts a user needs to have in order to get their info and display it. It was taken from WP_User_Query to exclude users with no posts (by @helgatheviking). I just made some minor changes to fit my ...


1

You can get rid of most of your code above. WP_User_Query has an include parameter (introduced in Wordpress 3.9) include (array) - List of users to be included. So we need to get a random array of author ID's. I have modified your count_users_with_posts() a bit here to extend it to make it more dynamic and to get a set amount of authors randomly. I ...



Only top voted, non community-wiki answers of a minimum length are eligible