Home Forums General Issues Query Posts by User ACF Field


Query Posts by User ACF Field

  • I have created a custom field (office_name) that displays on the user form. Each user is then able to choose the appropriate office with a select field:


    Office 1
    Office 2
    Office 3
    Office 4
    Office 5

    My goal is to create a query that matches all posts by each user that matches a specific office name. For example, if two users have “Office 1” selected, I would like to display all posts by those two users.

    I can accomplish this easily by manually coding the user ID’s as below:

    $office_listings = get_posts(array(
    	'author__in'     => array(1,2,4,6),
    	'posts_per_page' => -1
    if( $office_listings ):

    I’m hoping to find a dynamic way of doing this as to avoid having to manually update the code each time a new user is added to a specific office location.

    I did try something like it shows in the Docs:

    $posts = get_posts(array(
    	'numberposts'	=> -1,
    	'meta_key'	=> 'office_name',
    	'meta_value'	=> 'Office 1'

    This did not work.

    If anyone could help me out with this, it would be greatly appreciated.

  • There isn’t any way that you can do this with one query because you are adding locations to users. I’m not even sure you could do it in one query if you were adding users to locations.

    Locations added to users

    1. Get users
    2. Loop through users to get figure out who is at a the location and compile a list/array
    3. Query posts by users

    Users added to locations, this would be easier

    1. get location post
    2. Get list of authors from location
    3. query posts by list of authors

    Due to the way relationship fields are stored (this includes, relationship, taxonomy, user and any field that relates one WP object to another) as serialized arrays, and the fact that you can’t really query across multiple types of WP objects (multiple post types or users/post types) there isn’t going to be a way to do this with just a WP_Query and you’re going to need to do some kind of extra work and more than likely multiple queries to make it happen.

  • Thanks for this information John, much appreciated.

    Maybe for now I will just manually add the User IDs to create a simple query for each office.

  • Hi John, after thinking through the information you provided, I was in fact able to come up with an easy solution:

    $office_employees = get_users( array(
    	'meta_key' => 'office_name',
    	'meta_value' => 'Office 1',
    	'fields' => 'ID'
    $office_listings = get_posts(array(
    	'author__in'     => $office_employees,
    	'posts_per_page' => -1
    if( $office_listings ):


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

You must be logged in to reply to this topic.