簡體   English   中英

如何使用 where 子句使用自定義字段從數據庫中獲取記錄?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM