Home Forums Front-end Issues User Field – display relationship posts.


User Field – display relationship posts.

  • I’ve added a new “User field” in a CPT and wish to link the posts to specific users as I do not wish to change the author.

    How would you go in order to display these relationships? I can get it working in making a repeater to show all of the authors posts. Don’t know where to start in order to display the User Field relationships…

    The code i’m using to display the if usert = Author posts is the following.

    function dynamic_ids_query( $query ) {
    global $post;
    global $current_user;
    $authorID = $current_user->ID;
    if ( $query->query['post_type'][0] == 'taktiko-mitroo' or 'post' ) {
    $query->set( 'author', $authorID );
    add_action( 'pre_get_posts', 'dynamic_ids_query' );

    Any way to change it and make it to work for if user = user from the relationship?

  • You would have to alter the query by adding a meta_query for the user field.

    $meta_query = array(
        'key' => 'user_field_name',
        // user field is stored as a serialized array
        // of string User IDs
        // value must be enclosed in quotes
        // compare must be LIKE
        'value' => '"'.$user_id.'"',
        'compare' => 'LIKE'
    $query->set('meta_query', $meta_query);
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.