admin管理员组

文章数量:1392047

Suppose I have two categories A and B. Now I have uploaded some Posts A1, A2 under Category A and B1, B2 under Category B. Also for every Posts I have added some new custom fields using ACF plugin like thumbnail_image, first_image,second_image,third_image. I have added image for better understand below.

So if I want to retrieve all the Posts of Category A with their custom fields what is the Query.

Anybody help please ? thanks in advance

Suppose I have two categories A and B. Now I have uploaded some Posts A1, A2 under Category A and B1, B2 under Category B. Also for every Posts I have added some new custom fields using ACF plugin like thumbnail_image, first_image,second_image,third_image. I have added image for better understand below.

So if I want to retrieve all the Posts of Category A with their custom fields what is the Query.

Anybody help please ? thanks in advance

Share Improve this question edited Feb 3, 2020 at 6:30 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Feb 1, 2020 at 15:38 Arafat RahmanArafat Rahman 101
Add a comment  | 

1 Answer 1

Reset to default 0

You can use this code,

$args = array(
    'cat' => 4, // Category ID
    // 'category_name' => 'staff', // Category Name
    'posts_per_page' => -1,
)

// The Query
$query = new WP_Query( $args );

// The Loop
while ( $query->have_posts() ) {

    $query->the_post();

    $thumbnail_image = get_post_meta( get_the_ID(), 'thumbnail_image', true );
    $first_image     = get_post_meta( get_the_ID(), 'first_image',     true );
    $second_image    = get_post_meta( get_the_ID(), 'second_image',    true );
    $third_image     = get_post_meta( get_the_ID(), 'third_image',     true );
}

本文标签: Query for getting posts with their custom fields data in Wordpress