Support

Account

Home Forums Add-ons Flexible Content Field Query users by custom sub fields

Helping

Query users by custom sub fields

  • I have a flexible content field called ertekesites, which has a user subfield called tervezo_neve. I want to filter all the users whom has a value with the current post author’s ID in their tervezo_neve user subfield. But with this code, after i print_r i got an empty array. Where i’m going wrong? Thank you!

    function my_posts_where( $where ) {
    	
    	$where = str_replace("meta_key = 'ertekesites_$", "meta_key LIKE 'ertekesites_%", $where);
    
    	return $where;
    }
    
    add_filter('posts_where', 'my_posts_where');
    
    $args = array(
        'role' => 'Subscriber',
    	'meta_query'	=> array(
    		array(
    			'key'		=> 'ertekesites_$_tervezo_neve',
    			'compare'	=> '=',
    			'value'		=> get_the_author_meta('ID'),
    		),
    	)
    );
    
    $filtered_users = get_users($args);
    print_r($filtered_users);
  • You are attempting to filter the where clause for posts, the “post_where” hook is not called when getting users.

    To get users you would need to modify the WP_User_Query, but this class in WP does not have an equivalent hook to allow you to alter the WHERE part of the query.

    In any event, this would not work because the field you are trying to look at is on the POST and not on the USER and you cannot filter users by a value that is not associated with users.

    To get a list of users that are selected in a field on the post you would need to

    1) Loop over all the flex rows and compile a list of the selected usres
    2) Query the DB (_postmeta table) directly using $wpdp the get and then compile a list of users selected.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.