• Resolved TheXrion

    (@negarehgfx)


    I want to make json array of custom post type using wp_query.This function will echo post datas like the title and etc.. Is there any way to using wp_query to making an array like this :

    Note: I just put a sample for you to understanding my point.

    
    $query = new WP_Query($args);
    
        $dataArray = [];
        while ( $query->have_posts() ) : $query->the_post();
    
          $dataArray = array(
              'title' => the_title()
            );
    
    endwhile;
    
    $JSON = json_encode($dataArray);
    echo $JSON;
    • This topic was modified 2 years, 7 months ago by TheXrion.
Viewing 4 replies - 1 through 4 (of 4 total)
  • MK

    (@mkarimzada)

    You need to use array_push to push into $dataArray. Right now you are assigning value to a key that doesn’t exist.

    I hope this helps.

    Dion

    (@diondesigns)

    You probably something like this:

    $dataArray[] = ['title' => get_the_title()];

    Thread Starter TheXrion

    (@negarehgfx)

    @diondesigns Thanks dion, But i expected to find any information about it in wp_querydocumentation page. i didn’t know that i can use functions like get_the_title or get_the_ID using wp_query maybe i didn’t see.Is this information documented on wp_query page?

    Thanks

    Moderator bcworkz

    (@bcworkz)

    When you’re defining an args array to pass to WP_Query, you’re not really using WP_Query yet, you’re just defining a PHP array. You may assign values to an array element using any available functions. There’s no good example of this on the main WP_Query docs page, but the lack does not make it an invalid concept. It’s standard practice in PHP and all programming languages I’m familiar with, so we wouldn’t expect it to be covered in a WordPress specific class documentation.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using Wp_query to make json array’ is closed to new replies.