3

I try to fetch all posts from a custom post type in Wordpress and include the advanced custom fields (ACF) in the results as well, in order to generate a JSON file with the data.

$query = new WP_Query(array(
  'post_type' => 'resources',
  'post_status' => 'publish',
  'posts_per_page' => -1,
));

echo "var json=". json_encode($query->get_posts());

With a simple WP_Query, ACF data are not included and I have to iterate in the results and fetch all ACF manually one by one. Is there any way to include them in the original WP_Query results?

Tasos
  • 7,325
  • 18
  • 83
  • 176
  • define an array, use while loop to loop through each post, push the posts data that you want (including the ACF) to the array, json_encode the array. – Stender Jun 18 '20 at 10:45

3 Answers3

5

This would be my way of doing it.

Push whatever you want to the array and encode it.

<?php
$array = array();

$args         = array(
    'post_type'      => 'resources',
    'post_status'    => array( 'publish' ),
    'nopaging'       => true,
    'posts_per_page' => '-1',
    'order'          => 'ASC',
    'orderby'        => 'ID',

);
$queryResults = new WP_Query( $args );

if ( $queryResults->have_posts() ) {
    $counter = 0;
    while ( $queryResults->have_posts() ) {
        $queryResults->the_post();
        $array[ $counter ][ 'ID' ]           = get_the_ID();
        $array[ $counter ][ 'name' ]         = get_the_title();
        $array[ $counter ][ 'thumbnailURL' ] = get_the_post_thumbnail_url();
        $array[ $counter ][ 'place' ]        = get_field( 'resource_location' );
        //etc etc etc

        $counter++;
    }

    $jasoned = json_encode( $array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
    echo $jasoned;
} else {
    //nothing found
}
wp_reset_postdata();
koloml
  • 525
  • 2
  • 15
Stender
  • 2,446
  • 1
  • 14
  • 22
  • else, you can modify the query to include what you want, with a `pre_get_posts` action. write a function that pushes the meta data to the result and voila - your job is done. - read about it here : https://www.advancedcustomfields.com/resources/query-posts-custom-fields/#examples, under "Dynamic $_GET parameters" – Stender Jun 18 '20 at 11:14
  • Ok. I was thinking there might be a way to avoid the while loop, for performance. But it looks there isn't. Thanks. I will with your solution – Tasos Jun 18 '20 at 13:15
  • @Tasos As per my first answer. I had said the same thing that there is no solution except loop through post to get ACF feild. I am not sure how this solution helped you with what i said already in the first place. Good luck. – Always Helping Jun 18 '20 at 20:52
2

You can use get_fields() to fetch all acf fields registered at once with the post. Have a look the documentation here.

HW Siew
  • 973
  • 8
  • 16
  • Nice effort. But ‘get_feilds’ means there will be multiple lines of code to get field and it’s value. The best is to loop through it all and go from there. – Always Helping Jun 18 '20 at 10:54
  • You will only get which fields that are registered here, but not the value of the fields, right? – Stender Jun 18 '20 at 11:10
  • @Stender, you will get both key and value of the fields attached to the post at once. – HW Siew Jun 18 '20 at 13:42
0

To add ACF data in query. WP_Query will not help.

WP_Query does not return values from any custom fields. To get these you must loop through the posts and get the values of the fields.

Refer to this documentation: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

Always Helping
  • 14,316
  • 4
  • 13
  • 29