user2958073
user2958073

Reputation: 1

Getting author id

Im having some problems getting the author id inside of a archive page. I need to get the author id so i can then get the rest of the data.

I tried a lot of codes i found online inside and outside the loop. Everytime i only get the author id of the page where the archive is running from.

Here is a piece of the code i added to the template of the theme.

// Dealer Info
                                                        
$author_id = get_the_author_meta('ID');

$dealer_name = get_user_meta( $author_id, 'dealer_name', true );
$office_phone = get_user_meta( $author_id, 'office_phone', true );

$dealer_address = get_user_meta( $author_id, 'dealer_address', true );
$dealer_address_latitude = get_user_meta( $author_id, 'dealer_address_latitude', true );
$dealer_address_longitude = get_user_meta( $author_id, 'dealer_address_longitude', true );

if( empty($dealer_name)) {
$user = get_userdata( $author_id ); 
$dealer_name = $user->user_nicename;
}

a few lines beneath:

<div class="dealer-informatie">
<h4 class="dealer-titel"><?php echo $vehicle_location_name; ?></h4>
    authorid of post: <?php echo $author_id; ?><br>
    Telefoon: <?php echo $vehicle_location_phone; ?><br>
    E-mail: <?php echo $vehicle_location_email; ?><br>
    Adres: <?php echo $vehicle_location_address; ?>
</div>

I tried this and a lot of other things without result:

global $post;
$author_id = $post->post_author;

Thanks for reading!

Upvotes: 0

Views: 216

Answers (1)

Chris Haas
Chris Haas

Reputation: 55427

Okay, based on your last comment you have a custom query so you just need to setup the loop a little more explicitly:

// Your code, I just combined them into a single line
// You'll notice that `$cars` is gone that shouldn't be needed anymore
$cars_query = new WP_Query($search_args);

// See if we have anything
if ( $cars_query ->have_posts() ) {
     while ( $cars_query->have_posts() ) {

        // This sets up the global state so that "the loop" works
        $cars_query->the_post();

        // Your code goes here, and you will be inside of the loop at this point
        $car = get_post();
    }
 
} else {
    echo 'No cars found';
}

// Always a good idea when using custom queries
wp_reset_postdata();

EDIT

To be clear, the above code replaced your previous for loop. So previously you had if ( ! $cars ) : and in the new code it is if ( $cars_query ->have_posts() ). Similarly, you previously had foreach ( $cars as $car ) and the new code has while ( $cars_query->have_posts() ).

That should be the only things you need to update, hopefully.

Upvotes: 1

Related Questions