Support

Account

Home Forums General Issues Working with Post Object in relation to User

Solved

Working with Post Object in relation to User

  • Hello. I’m trying to utilize fields from a Custom Post type that is selected on a User’s profile page using a Post Object field on the user profile, and output them in relation to a user’s author profile on a post. Specifically the permalink and post_content, but maybe other fields.

    I have figured out how to link an Author’s name to that custom post type using the Page Link field working with this code.

    $author_id = get_the_author_meta('ID');
     $author_bio = get_field('page_link_field', 'user_'. $author_id );
    
    echo '<a href="' . $author_bio . '">';
    the_author();
    echo '</a>';

    But the Post Object is a little confusing on how I get it to output the different fields for the linked page object, based on author ID. Using this has shown me all the data

    echo '<pre>';
        print_r( get_field('post_object_field', 'user_'. $author_id)  );
    echo '</pre>';
    die;

    but I’m not sure how to get certain parts and I don’t see permalink or any URL to the post.

    Thanks for any help.

  • I changed over to Relationship as Post Object and that got me what I needed.
    Here is a sample that might help get you started.

    $author_id = get_the_author_meta('ID');
    $posts = get_field('field1_name', 'user_'. $author_id);
    if( $posts ):
    foreach( $posts as $post): // variable must be called $post (IMPORTANT) 
    setup_postdata($post);
    echo '<a href="' . the_permalink() . '">' . the_title() . '</a>';
    the_field('field2_name');
    the_excerpt();
    endforeach;
    wp_reset_postdata();  // IMPORTANT - reset the $post object so the rest of the page works correctly

    Maybe this helps someone else.

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

The topic ‘Working with Post Object in relation to User’ is closed to new replies.