Support

Account

Home Forums Backend Issues (wp-admin) Post Object – only query current user

Solving

Post Object – only query current user

  • Is there a way to query a current user’s posts when using a Post Object field?

    Example, Post Object is set to custom post type of music. User has created 5 different music posts. They can create a custom post type of album and select with post object X number of songs. I don’t want to populate the post object with songs from everyone, only their own.

    Building on this, I’m also wondering if it’s let’s say they create another album if I can limit the Post object results (songs) to only those that are theirs AND they have not previously selected in another album.

  • I’d also love to know this. I’d like to create collections of posts by the current user.

  • I think I’m almost there using a custom filter function in functions.php

    function my_post_object_query( $args )
    {
    
        $args['author'] = '2';
    
        return $args;
    }
    
    add_filter('acf/fields/post_object/query/name=my_post_object_field_name', 'my_post_object_query', 10, 3);

    The only thing I need to figure out is, how to dynamically set the ID to the current user/author.

  • That seems to do the trick:

    function my_post_object_query( $args )
    {
        $args['author'] = get_current_user_id();
    
        return $args;
    }
    
    add_filter('acf/fields/post_object/query/name=my_post_object_field_name', 'my_post_object_query', 10, 3);

    ACF rocks!!

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

The topic ‘Post Object – only query current user’ is closed to new replies.