Support

Account

Home Forums Backend Issues (wp-admin) Sorting a column for a user field

Solved

Sorting a column for a user field

  • Hey there!

    I’m trying to figure out how to sort a column for a user field.

    First of all, is a user field a thing in ACF (Pro)? Not a user meta field or whatever but an actual field type that lets you select users. I’m not 100% sure since I couldn’t find it in the ACF docs anywhere. It may have been custom created for this client’s site and if so then I suppose I am likely on my own to figure this out.

    The user field seems to store user id #s followed by the word required (i.e. 32 required) but sometimes it is json encoded data (i.e. a:1:{i:0;s:3:”281″;}). I don’t really understand that either.

    When I added a new column in the admin to display the usernames with the list of posts for this custom post type they do sort, but not correctly, and I’m guessing that’s because it’s sorting by this data instead of the usernames which are output to the column.

    The question is then how can I make it sort by the username rather than what’s in the database?

    Here is the code for column sorting I found somewhere on these forums:

    
    add_filter('pre_get_posts', 'custom_column_orderby');
    function custom_column_orderby($query) {
    	if (!is_admin() || !$query->is_main_query()) {
    		return;
    	}
    	if ($query->query_vars['post_type'] == 'custom_post_type' && ($orderby = $query->get('orderby'))) {
    		switch ($orderby) {
    			case 'custom_field':
    				$query->set('meta_key', 'custom_field');
    				$query->set('orderby', 'meta_value_num');
    				break;
    			default:
    				break;
    		}
    	}
    }
    

    Thank you!

  • first i have to ask what version of ACF are you using. You mention that one of the db fields looks like 32 required and the required part has be confused, that should not be there. The user field is a field in ACF and it stores user ID values. The difference between a single value 32 and something that looks like this a:1:{i:0;s:3:”281″;}) is whether or not the field allows multiple selections of only a single selection. The second value is a field that allows multiple selections and stores an array of user IDs and what you’re seeing is a serialized value. How you search posts based on the value will depend on what type whether or not your searching a field that allows multiple values or not. If you’re searching a field that uses a single value then you can search for just the value, but if it’s one that allows multiple then you need to use something like LIKE '"'.$user_ID.'"'

    The shorting problem is going to be a bit more difficult, and that’s also going to depend on what type of user field you’re searching, there’s too many variables at this point to begin helping you sort that part out.

    The first thing we have to do is nail down if the field you’re searching allows 1 or multiple values.

  • Hi there,

    Sorry it took so long to get back to this! Have been awaiting approval to move ahead but now have it.

    At present the site has ACF Pro Version 5.3.7. This site is usually keeping up with the latest ACF Pro version fairly closely, though lags behind sometimes for a number of weeks until we get time to update and test everything. It’s been using it for a fair amount of time so it could have been caused by an older version. I probably could get approval to update it to the latest version which at this time appears to be 5.3.9.2.

    By the sounds of it maybe there was a bug at one point somewhere or another which affected the fields in the db. I’m not sure if in the code on the site or ACF or what but if the word “required” is showing up in that field in the db something definitely seems wrong somewhere if it shouldn’t be there. So if I remove the word required and take the single and multi-selection values into consideration then maybe it will sort properly. I will test that out. Thanks!

    It does allow selecting multiple values in the field settings.

    The field is a User field type.

  • I figured maybe the old single valued fields with “required” in them were probably just messed up so I converted them all to the same format and they seem to work correctly. But now I’m still not 100% sure how to go about sorting them properly. I suppose maybe I just have to get their username associated with the query somehow and sort by that.

    Thanks for your help =)

  • To be honest, I don’t think it’s possible to sort posts by a user field unless you want to sort them by user ID. My suggestion would be to create an acf/save_post filter https://www.advancedcustomfields.com/resources/acfsave_post/. In this filter get the user from the ACF field and create a value in a separate custom field using a standard WP update_post_meta() call an add the value you want to sort by to this other field. Then you’d be able to use this field in your query for ordering the posts.

  • You are a genius! That sounds like it’ll do the trick nicely. Thank you for taking the time to share your knowledge!

  • /**
     * acf/save_post filter to create custom sortable usernames field
     * 
     * @param array $post_id
     */
    function my_acf_save_post( $post_id ) {
        
        // get users from ACF field
        $my_users = get_field('my_users');
    	
    	// get usernames
    	$usernames = array();
    	foreach($my_users as $my_user) {
    		$usernames[] = $my_user['nickname'];
    	}
    	$value = implode(", ", $usernames);
    	
    	// add usernames to sortable field
    	update_post_meta($post_id, 'my_users_sortable', $value);
    }
    
    add_action('acf/save_post', 'my_acf_save_post', 20);
    
  • Another bit of code to go with the previous one that updates all previously created posts of type my_custom_post_type with the extra meta data instead of having to manually do it.

    /**
     * Update post meta for sortable column
     */
    if (is_admin() && isset($_GET['addsortablemeta']) && $_GET['addsortablemeta'] == 1) {
    	$args = array(
    		'post_type' => 'my_custom_post_type',
    		'nopaging' => true,
    	);
    	$my_query = null;
    	$my_query = new WP_Query($args);
    	if ($my_query->have_posts()) {
    		while ($my_query->have_posts()) : $my_query->the_post();
    			my_acf_save_post($post->ID);
    		endwhile;
    	}
    	wp_reset_query();
    }
Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Sorting a column for a user field’ is closed to new replies.