[英]How to fetch the records from the database with a custom field using where clause?
我的頁面上有 20 個帖子,每個帖子都有一個名為is_feature_home
的自定義字段。 我必須獲取檢查is_feature_home
的帖子。
我嘗試了以下代碼,但沒有得到正確的 output
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.