ztukaz
ztukaz

Reputation: 11

Wordpress get title inside loop

I am trying to get title inside following code

$args = array(
          'post_type' => 'post',
          'post_status' => 'draft',
      'posts_per_page'   => 9,
          );
// get the draft post         
$posts_array = get_posts( $args ); 
foreach($posts_array as $post){
    // foreach draft post get title, content and category
    $post_id = $post->ID;
    $postitle = $post->post_title;
    $content_page = get_post_field('post_content', 1);
    $content_post = get_post_field('post_content', $post->ID);
    $pag_e = array(
            'ID'           => 1,
            'post_content' => $content_post.$postitle.$content_page,
        );
    wp_update_post( $pag_e );
    wp_delete_post( $post->ID, true );
}

I have tried using also

$postitle = get_the_title($post->ID);
$postitle = the_title();
$postitle = get_post_field('post_title', $post->ID);

After that i have to get category also.

Thank you for your help in advance

Upvotes: 0

Views: 2860

Answers (1)

rnevius
rnevius

Reputation: 27112

You should consider using setup_postdata():

$args = array(
    'post_type' => 'post',
    'post_status' => 'draft',
    'posts_per_page'   => 9,
);      
$posts_array = get_posts( $args ); 

foreach($posts_array as $post){
    // Setup the global post data
    setup_postdata( $post ):

    $postitle = get_the_title();

    // Reset post data
    wp_reset_postdata();
}

Upvotes: 2

Related Questions