我正在尝试创建一个快捷码,它基于Yoast的主要类别元键运行自定义查询。
目标是使用与所显示的帖子相同的元键值来拉帖子,并在侧边栏中显示4个以上的帖子。
到目前为止,一切正常,但查询没有得到具有相同值的帖子。
我添加了一个元键值的回声,以确保它得到了它,而且它确实得到了。
这是我的代码:
function primary_category_query_shortcode( $atts ) {
$atts = shortcode_atts( $defaults, $atts, \'primary_category_query\' );
$my_meta = get_post_meta ( $id, \'_yoast_wpseo_primary_category\', true);
// WP_Query arguments
$args = array (
\'posts_per_page\' => \'4\',
\'meta_query\' => array(
array(
\'key\' => \'_yoast_wpseo_primary_category\',
\'value\' => \'$my_meta\',
),
),
);
echo get_post_meta( get_the_ID(), \'_yoast_wpseo_primary_category\', true);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) { ?>
<section class="recent-posts clear">
<?php while ( $query->have_posts() ) : $query->the_post() ; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( \'left\' ); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</article>
<?php endwhile; ?>
</section>
<?php } else {
echo \'Sorry, no more found.\';
}
/* Restore original Post Data */
wp_reset_postdata();
}
?>```