The main issue is that you’ve mixed up two methods of querying posts.
You’re querying the correct posts here:
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'assessment',
'meta_key' => 'customer',
'meta_value' => $current_user_id
));
But then the posts you’re actually looping through are queried here:
$the_query = new WP_Query( $args );
Where $args
is undefined and has nothing to do with the previously queried posts. You can pass the same arguments to WP_Query
as get_posts()
, but with posts_per_page
, rather than numberposts
:
function lista_mis_valoraciones_shortcode() {
$current_user_id = get_current_user_id();
$the_query = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'assessment',
'meta_key' => 'customer',
'meta_value' => $current_user_id,
)
);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field( 'event_thumbnail' ); ?>" />
<?php the_title(); ?>
<?php
endwhile;
}
wp_reset_postdata();
}
add_shortcode( 'lista_mis_valoraciones', 'lista_mis_valoraciones_shortcode' );
Note the other changes I made:
- You had mixed in HTML with PHP with the
<img>
and<a>
tags. To output HTML you need to close the PHP tags with?>
and open them again with<?php
when you’re done. - You had a stray
endif
that needed to be removed. wp_reset_postdata()
is more appropriate in this case thanwp_reset_query()
.