Support

Account

Home Forums General Issues Post Object Query By Author

Solved

Post Object Query By Author

  • I am trying to limit the Post Object selects either by Author or By User Can Edit.

    I wish to allow users only to select pages they can edit from Post Object. I currently have this:

    function my_post_object_query( $args, $field, $post )
    {
    // modify the order
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $args[‘post_type’]=”franchises”;
    $args[‘author’]=$user_id;

    return $args;
    }

    // filter for every field
    add_filter(‘acf/fields/post_object/query/name=franchise’, ‘my_post_object_query’, 10, 3);

    The Post Type limiter works but not the Post Author.

  • use author instead of post_author

  • I originally had author however that still doesn’t work. I have changed it back to author now.

  • yeah i found =)

    post object field uses get_posts for all post types and get_pages for pages
    so if you want to filter them both

    use

    
    $args['author']=$user_id;
    $args['authors']=$user_id;

    but if you using custom post type why you didnt set the post type filter in the custom field options?

  • Perfect thank you, I only put the post type filter in there as well so I could see if the whole thing was working.

  • excuse me,
    I need the same function.
    or at least I think it is the same:
    I need that in the “post_object” seen in the admin side, you see a list of our posts in a specific custom post, obviously without seeing those of others.

    I copied the code and I replaced the custom post with my “franchises” -> “stand”

    But by testing with user semplicesi continues to see the full list.
    What I’m missing?

  • did you also changed

    // filter for every field
    add_filter(‘acf/fields/post_object/query/name=franchise’, ‘my_post_object_query’, 10, 3);
    

    name=franchise with your field slug?

  • my custom post is : ‘stand’

    function my_post_object_query( $args, $field, $post ) {
    // modify the order
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $args['author']=$user_id;
    $args['authors']=$user_id;
    $args['post_type']='stand';
    return $args;
    }
    
    // filter for every field
    add_filter('acf/fields/post_object/query/name=stand', 'my_post_object_query', 10, 3);
  • and what is your posts custom field name?
    add_filter(‘acf/fields/post_object/query/name=stand‘) <= not post type name, but ACF custom field name

  • here’s what I had missed.
    Yes, now it works properly!
    thanks so much

  • I was wondering,
    you can set the filter applies only to users and not admins or moderators?

    I tried to use this code to differentiate between users and admin, but it is as if nothing is filtering.

    if ( ! is_admin() ) {
             // modify the order
             $current_user = wp_get_current_user();
             $user_id = $current_user->ID;
             $args['author']=$user_id;
             $args['authors']=$user_id;
             $args['post_type']='stand';
             return $args;
    } 
    else { $args['post_type']='stand'; return $args; }
    

    There is solution?
    Thanks

  • is_admin

    Description
    This Conditional Tag checks if the Dashboard or the administration panel is attempting to be displayed. It should not be used as a means to verify whether the current user has permission to view the Dashboard or the administration panel (try current_user_can() instead). This is a boolean function that will return true if the URL being accessed is in the admin section, or false for a front-end page.

    Usage

    <?php is_admin(); ?>

  • if ( !current_user_can('manage_options')) {
             // modify the order
             $current_user = wp_get_current_user();
             $user_id = $current_user->ID;
             $args['author']=$user_id;
             $args['authors']=$user_id;
             $args['post_type']='stand';
             return $args;
    } 
    else { $args['post_type']='stand'; return $args; }
  • correct as always,
    Thanks!

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

The topic ‘Post Object Query By Author’ is closed to new replies.