Support

Account

Home Forums Front-end Issues How to Filter WP_User_Query based on Post Object

Solved

How to Filter WP_User_Query based on Post Object

  • Hi,

    I have gone through many forums answers but not find an exact solution so I am raising my own question to get a fast response.

    I have created a custom field for users as “Post Object” and its multi-select.

    Now in the frontend of my template, I want to filer users grid, based on the selected “Post Object” title.

    Here is the query that I am trying to work out.

    $user_query = new WP_User_Query(array(
            'role' => 'teacher',
            'number' => 16,
            'offset' => $offset,
            'orderby' => 'display_name',
            'order' => 'ASC',
            'meta_query' => array(
                array(
                    'key' => get_field('instruments_offered')->title, // trying to filter by post object title
                    'value' => $resource_cat,
                    'compare' => '='
                )
            )
        ));

    I hope I am a bit clear about what I want to achieve, please guide me to resolve it.

    Thanks in advance.

  • You cannot do what you are attempting to do.

    The issues
    1) A post object fields stores the post ID of the post object selected
    2) When set to allow multiple selections it stores a serialized array of post IDs

    The first thing you need to do is know the post ID of the post object you want to filter by

    
    'meta_query' => array(
      array(
        'key' => 'POST OBJECT FIELD NAME',
        'value' => '"'.$post_id.'"'. // the post id that you are looking for
        'compare' => 'LIKE'
      )
    )
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.