如何使用 where 子句从数据库中获取带有自定义字段的记录?
How to fetch the records from the database with a custom field using where clause?
我的页面上有 20 个 post,每个 post 都有一个名为 is_feature_home
的自定义字段。我必须获取检查 is_feature_home
的 post。
我尝试了下面的代码,但没有得到正确的输出
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();
}
我的页面上有 20 个 post,每个 post 都有一个名为 is_feature_home
的自定义字段。我必须获取检查 is_feature_home
的 post。
我尝试了下面的代码,但没有得到正确的输出
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();
}