我需要获取所有帖子ID、帖子标题和相应的meta\\u值,该值是自定义帖子类型的“特色”。我想我需要加入,但我不知道他们是怎么工作的。。谁能帮我一下吗?
Combine posts and postmeta
2 个回复
SO网友:Hady Shaltout
您可以使用get\\u posts内置函数更改meta\\u键和meta\\u值
$args = array(
\'meta_key\' => \'YOUR_META_KEY\',
\'meta_value\' => \'featured\',
\'post_type\' => \'YOUR_POST_TYPE\',
\'post_mime_type\' => \'\',
\'post_parent\' => \'\',
\'author\' => \'\',
\'author_name\' => \'\',
\'post_status\' => \'publish\',
\'suppress_filters\' => true
);
$posts_array = get_posts( $args );
foreach ( $posts_array as $post ) : setup_postdata( $post ); ?>
<li>
<span><?php echo esc_attr( $post->ID ); ?></span>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach;
// MUST Reset
wp_reset_postdata();
注意:不要修改本地foreach变量[$post]有关更多详细信息,请参阅WordPress get_posts function
SO网友:inarilo
$args = array(
\'post_type\' => \'my_custom_post_type\',
\'meta_query\' => array(
array(
\'key\' => \'name_of_field_with_featured_as_value\',
\'value\' => \'featured\',
\'compare\' => \'=\',
),
),
);
$query = new WP_Query($args);
if($query->have_posts()) :
//usual loop code but with $query as above, note that post related functions are called as usual, e.g. simply the_title(), not $query->the_title()
endif;
结束