0

I have a post type "contracts" and on a specific page template want to show posts where the meta_key "distributor_email" matches the current user's email. Have tried both of the following and both return a 500 internal server error. Which probably means I'm missing something pretty basic but I haven't been able to figure it out and I would really appreciate another set of eyeballs on this.

            <?php
            $userEmail;
            if ( wp_get_current_user() instanceof WP_User ) {
                    $userEmail = wp_get_current_user()->user_email;
            }
            $users_query = new WP_Query( array(
                    'post_type' => 'contracts',
                    'meta_key' => 'distributor_email',
                    'meta_value' => $userEmail,
            ) ); ?>

<?php if ( $users_query->have_posts() ): ?>

                <?php while ( $users_query->have_posts(): $users_query->the_post() ) ?>
                <div class="loginContract">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="pink"><?php the_title(); ?></a>
                        <?php the_content(); ?> 
                </div>      
                <?php endwhile; ?>

        <?php endif; ?>

And this is the second one I tried..

            <?php
            $userEmail;
            if ( wp_get_current_user() instanceof WP_User ) {
                    $userEmail = wp_get_current_user()->user_email;
            }
            $users_query = new WP_Query( array(
                    'post_type' => 'contracts',
                    'meta_query' => array(
                        array(
                            'key'     => 'distributor_email',
                            'value'   => $userEmail,
                            'compare' => '=',
                        ),
                    ),

            ) );


        ?>
<?php if ( $users_query->have_posts() ): ?>

                <?php while ( $users_query->have_posts(): $users_query->the_post() ) ?>
                <div class="loginContract">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="pink"><?php the_title(); ?></a>
                        <?php the_content(); ?> 
                </div>      
                <?php endwhile; ?>

        <?php endif; ?>

Thank you :)

  • I expect the first line would throw a syntax error. Have you enabled debugging? Then you'll get error messages telling you which line the problem is in. – Andy Macaulay-Brook Sep 24 '16 at 18:49
0

Both examples of your code throw errors on the while line:

<?php while ( $users_query->have_posts(): $users_query->the_post() ) ?>

The while will work if you move the parenthesis from the end to before the colon, and replace the parenthesis with a semicolon.

<?php while ( $users_query->have_posts() ) : $users_query->the_post() ; ?>

(The Errors turn up fast on WP PHP Console: Don't stay home without it... I've gotten into the habit of testing this stuff out using it with Shortcode Exec PHP.)

Your Answer

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

Not the answer you're looking for?Browse other questions tagged or ask your own question.