skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

    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 ?>
    

  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search