Php 如何使用where子句使用自定义字段从数据库中获取记录?
我的页面上有20篇文章,每篇文章都有一个自定义字段,名为Php 如何使用where子句使用自定义字段从数据库中获取记录?,php,wordpress,custom-fields,Php,Wordpress,Custom Fields,我的页面上有20篇文章,每篇文章都有一个自定义字段,名为is\u feature\u home。我得把检查过的帖子拿到家里去 我尝试了下面的代码,但没有得到正确的输出 function getpagecompany(){ global $post; $args = array( 'post_type' => 'company', 'post_status' => 'publish', 'posts_per_page' =&
is\u feature\u 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();
}
@CBroe,谢谢分享链接。这是一个很好的学习链接。让我试试这个答案是的,它显示了正确的输出。如果我必须使用多个自定义字段,那么我必须重复meta_键代码。。对吗?不,您需要传入类似数组的命令:“meta_query”=>array(array('key'=>'key1','value'=>'value1'))谢谢您的帮助。我试着获取标题()和内容(),结果成功了。
/* 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();
}