Support

Account

Home Forums General Issues Use get_field for data from particular post AND specific author

Solved

Use get_field for data from particular post AND specific author

  • Hello. I’m using get_field to display some data from a particular post on another page, this is working as expected user get_field($field_name, $post_id).

    However, I also want to check whether the post_id I’m referencing has the current user as the author. If so, display the data, if not, don’t. I see in the documentation that $post_id can target a specific user (user_#) but I’m not sure how to set up the query to support post_id such that it can reference a post ID that’s not the current post, in addition to matching the author to the logged in user.

    Thanks.

  • Hi @fontsnob

    You can use get_post() and wp_get_current_user() to check the author and the current user. Something like this:

    $otherPost = get_post($post_id);
    $currentUser = wp_get_current_user();
    if($otherPost->post_author == $currentUser->ID){
        //do something if current user is the post author
    }

    Hope this helps.

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

The topic ‘Use get_field for data from particular post AND specific author’ is closed to new replies.