0

I try to show posts for all registered users. The users have some user_meta fields, that i can display suggested posts for them. I have exact the same function in use on the profile page of a user, and its working there. Here i need to loop through all users, but i dont get any posts returned. When i comment out the whole tax_query argument, the posts are shown. Does anyone have a clue, why this isnt working?

function leweb_send_suggested_events() {

    $users = get_users( [ 'role__in' => [ 'um_musiker_blech', 'um_musiker_bronze', 'um_musiker_silber', 'um_musiker_gold' ] ] );
    // Array of WP_User objects.
    foreach ( $users as $user ) {

        // Benötigte Userdaten
        $user_id = $user->id;
        $current_artist = get_user_meta( $user_id, 'um_musikkategorie', true);
        $region = get_user_meta( $user_id, 'um_gebiete');   

        // DEBUG
        echo 'ID: ' . $user_id . ' ARTIST: '. $current_artist . '<br />';

        // Query
        global $wp_query;

        // Arguments  
        $args = array(
            'post_type' => 'events',
            'post_status' => 'publish',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'leweb_ct_musiker',
                    'field'    => 'slug',
                    'terms'    => $current_artist,
                ),
                array(
                    'taxonomy' => 'leweb_ct_standort',
                    'field'    => 'name',
                    'terms'    => $region[0],
                ),
            ),
        );  

        $query = new WP_Query( $args );

        if( $query->have_posts() ) {

            // DEBUG
            echo '<pre>' . print_r( $query ) . '</pre>';

            echo '<div class="table-2 suggested-events-widget"><table><tbody>';

            while( $query->have_posts() ) {
                $query->the_post();

                // Post Meta
                $event = get_post_meta( get_the_ID(), 'event', true );
                $artist = get_post_meta( get_the_ID(), 'event_musiker', true );
                $event_standort = get_post_meta( get_the_ID(), 'event_standort', true );
                $event_plz = get_post_meta( get_the_ID(), 'event_plz', true );
                $event_stadt = get_post_meta( get_the_ID(), 'event_stadt', true );
                $event_timestamp = get_post_meta( get_the_ID(), 'event_date_timestamp', true );
                $event_link = get_post_permalink( get_the_ID() );

                // Zeit Formatierung
                $day = date_i18n( "d", $event_timestamp );
                $month = date_i18n( "M", $event_timestamp );

                // Tabelle
                echo '<tr>';
                echo '<td class="event-date"><a href="' . $event_link . '"><span class="month">' . $month . '</span><span class="day">' . $day . '</span></a></td>';
                echo '<td class="event-meta"><a href="' . $event_link . '"><span class="event">' . $event . '</span><span class="place">' . 'In ' . $event_standort . ', ' . $event_plz . ' ' . $event_stadt . '</span></a></td>';
                echo '</tr>';
            }

            echo '</tbody></table></div>';



        }

        wp_reset_postdata();

    }
}

leweb_send_suggested_events();
  • If the tax_query is giving you troubles, then it means there are no posts found that matched these criteria: 1) In a leweb_ct_musiker taxonomy where the term slug is the $current_artist value; and 2) That it's also in a leweb_ct_standort taxonomy where the term name is the $region[0] value. You can try changing the relation to OR. And are you sure $current_artist and $region[0] are valid terms? – Sally CJ Nov 6 '19 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.