问题描述
我的页面上有 20 个帖子,每个帖子都有一个名为 is_feature_home
的自定义字段。我必须提取 is_feature_home
选中的帖子。
function getpagecompany(){
global $post;
$args = array(
'post_type' => 'company','post_status' => 'publish','posts_per_page' => 8,'is_feature_home'=>1,);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$tid=$loop->ID;
print_r(the_title);
// print_r(post_content);
endwhile;
wp_reset_postdata();
}
解决方法
/* you must fetch post by meta key so please change your code with below */
function getpagecompany(){
global $post;
$args = array(
'post_type' => 'company','post_status' => 'publish','posts_per_page' => 8,'meta_key' => 'is_feature_home','meta_value' => '1','meta_compare' => '=' // default operator is (=) equals to
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$tid=$loop->ID;
print_r(the_title);
// print_r(post_content);
endwhile;
wp_reset_postdata();
}