WordPress Query Posts then put into Array

I want to have output of an WP_Query to an array.for example an array of my last 10 post titles or an array of last post urls

Related posts

Leave a Reply

1 comment

  1. If you want to collect the results from WP_Query() into another array, you can try this:

    $my_array=array();
    
    // the query
    $args=array('post_type' => 'post','posts_per_page'=>10,'orderby'=>'date', 'order'=>'ASC');
    $my_query = new WP_Query($args);
    
    if ($my_query->have_posts()) : 
        while ($my_query->have_posts()) : $my_query->the_post(); 
            $my_array[]=get_the_title(get_the_ID());
        endwhile;
    endif;
    
    // debug: 
    print_r($my_array);
    print_r($my_query);
    

    And this example will give you the 10 last post titles into the array $my_array()