主要问题是您混淆了两种查询帖子的方法。
您正在此处查询正确的帖子:
$posts = get_posts(array(
\'numberposts\' => -1,
\'post_type\' => \'assessment\',
\'meta_key\' => \'customer\',
\'meta_value\' => $current_user_id
));
但是,您实际循环浏览的帖子将在此处查询:
$the_query = new WP_Query( $args );
在哪里
$args 未定义,与之前查询的帖子无关。您可以将相同的参数传递给
WP_Query 像
get_posts(), 但是有
posts_per_page, 而不是
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\' );
请注意我所做的其他更改:
您将HTML与PHP混合在一起<img> 和<a> 标签。要输出HTML,需要使用关闭PHP标记?> 然后用<?php 完成后你有个流浪汉endif 需要移除的
wp_reset_postdata() 在这种情况下比wp_reset_query().