Question

I'm trying to query post by the user id. The user id in this case will be the author of another post.

I did try the following, however it's not getting the 'author argument'.

$author_id = $post->post_author;

$author_query_id = array('author='. $author_id, 'showposts' => '1', 'post_type'=> 'bedrijven', 'post_status' => 'any');
$author_posts_id = new WP_Query($author_query_id);

while($author_posts_id->have_posts()) : $author_posts_id->the_post();


if (get_field('standplaats')) { ?>
<div class="fieldje field-3"><p>
        <span>Standplaats: <?php echo the_field('standplaats'); ?></span>
    </p></div>
 <?php  } 
 endwhile; 

The user id I'm getting is correct (captured in $author_id). However in the query it just queries the last post of the post type 'Bedrijven', no matter who the author is.

Any idea why this is not working?

Thanks!

Était-ce utile?

La solution

Your array is formatted incorrectly.

$author_query_id = array('author' => $author_id, 'showposts' => '1', 'post_type'=> 'bedrijven', 'post_status' => 'any');

I removed the = from your author key.

I'm sure you've seen the WP_Query documents below, but here is a link to help in the future, just in case.

Autres conseils

use this instead:

$author_query_id = array('author'=> $author_id, 'showposts' => '1', 'post_type'=> 'bedrijven', 'post_status' => 'any');

Use This Code:

<?php

$args = array(
'post_type' => 'bedrijven',
'post_status' => 'publish',
'author'        =>  $current_user->ID,
'orderby'       =>  'post_date',
'order'         =>  'ASC',
'posts_per_page' => 1
);


$author_query = new WP_Query( $args ); 

while ( $author_query->have_posts() ) : $author_query->the_post();

$standplaats = get_field('your_field');

if ($standplaats) { ?>

<div class="fieldje field-3">
<p>
<span>Standplaats: <?php echo $standplaats ; ?></span>
</p>
</div>

<?php  }  endwhile; ?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top