0
votes

I am trying to display users attached to a Custom Post Type. I created an ACF-Users-Field which returns the ID. This field is displayed at my Custom Post Type. Now I would like to display the names of the users selected in this field.

I am able to display all names of my users (if I remove the part 'meta_query' of my arguments, but if i add the meta_query array to my arguments nothing is displayed. I am not sure, maybe the mistake is the 'value', but I have no idea what to change there. Below I show the relevant code:

<article>
            <?php
            $args = array(
                'role' => 'Subscriber',
            );

            $my_user_query = new WP_User_Query( $args );
            $editors = $my_user_query->get_results();


            if ( ! empty( $editors ) ) {

                foreach ( $editors as $editor ) {

                    $editor_info = get_userdata( $editor->ID );

                    $args = array(
                        'post_type' => 'projekt',
                        'meta_query' => array(
                            array(
                            'key' => 'projektteilnehmer', // name of custom field - return ID
                            'value' => $post->ID ,
                            'compare' => 'LIKE'
                            )
                        )
                    );

                    // The User Query
                    $user_query = new WP_User_Query( $args );
                    // The User Loop
                    if ( ! empty( $user_query->results ) ) { ?>

                        <?php foreach ( $user_query->results as $user ) {
                            echo $user->user_firstname;;
                        }
                    } else {
                        echo 'nothing found';
                    }

                } // endforeach

            } else {
                echo __( 'Kein Mitglied gefunden.' );
            }

            ?>
        </article>

Any help appreciated

2
May be something small, do you want to select users with the Subscriber role ? or perhaps you are trying to select the editor role ? - Jasper B
I would like to select users with the Subscriber role, this is working. But not all subscribers, only the subscribers which I select in my ACF-Field 'projektteilnehmer' in the Custom Post Type. - maariaa
ok, looking a bit closer at your code I'm confused. you are currently looping trough the users, then you create a array for a post_query and use these arguments for a user query. So, if im understanding correct your are displaying all projekt posts on a page and in this prject you want to show all users that are involved in the project stored in a AFC field named projektteilnehmer - Jasper B

2 Answers

0
votes

By what your describing this code should do what you want. given that the participant's are returned as userID

<article>
    <?php
    $teilnehmers = get_field('projektteilnehmer', $post->ID);


    if ( ! empty( $teilnehmers ) ) {
      foreach ( $teilnehmers as $teilnehmer ) {
          $user = get_user_by('ID', $teilnehmer);
          if($user){
            echo $user->user_firstname;;
          }
      }
    } else {
      echo __( 'Kein Mitglied gefunden.' );
    }
    ?>
</article>

or if they are returned as user objects

<article>
    <?php
    $teilnehmers = get_field('projektteilnehmer', $post->ID);


    if ( ! empty( $teilnehmers ) ) {
      foreach ( $teilnehmers as $teilnehmer ) {
            echo $teilnehmer->user_firstname;;
      }
    } else {
      echo __( 'Kein Mitglied gefunden.' );
    }
    ?>
</article>
0
votes

The answer of Jasper B pushed me in the right direction. Thank you very much:) It is working with this query:

<?php // projekte
                            $projekte = get_posts(array(
                                'post_type' => 'projekt',
                                'meta_query' => array(
                                    array(
                                      'key' => 'projektteilnehmer', // name of custom field - return value id
                                      'value' => $editor_info->ID,  
                                      'compare' => 'LIKE'
                                    )
                                )
                            
                            ));
                            ?>
                            
                            <?php if( $projekte ): ?>
                            <strong>Projekte:</strong>
                                <ul>
                                <?php foreach( $projekte as $projekt ):?>
                                        <li>
                                            <a href="<?php echo get_permalink( $projekt->ID ); ?>">
                                                <?php echo get_the_title( $projekt->ID ); ?>
                                            </a>
                                        </li>
                                <?php endforeach; ?>
                                </ul>
                            <?php endif; 
                            // ende projekte ?>