0

I have 20 posts on my page and each post has a custom field called is_feature_home. I have to fetch the post where is_feature_home checked.

I tried the below code but I am not getting the correct 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(); 
}
2

1 Answer 1

1
/* 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(); 
}
Sign up to request clarification or add additional context in comments.

6 Comments

Let me try this answer
Yes, it's displaying the correct output. If I have to use the more than 1 custom field then I have to repeat the meta_key code.. right?
like: 'meta_query' => array( array( 'key' => 'key1', 'value' => 'value1' ) )
let me know if you need more clarification for multiple fields
Thanks for the help. i tried get_the_title() and get_the_content() and it's working.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.