Savan Paun
Savan Paun

Reputation: 1733

How to get all post list in wordpress admin?

i am creating a plugin. i want to get all post like title and url. Not on frontpage just admin panel. when i try to use this but not working

<?php
$args = array( 'numberposts' => -1); 
$posts= get_posts( $args );
if ($posts) {
    foreach ( $posts as $post ) {
        setup_postdata($post);
        the_title();
    }
}
?>

Upvotes: 2

Views: 3913

Answers (2)

Vidya L
Vidya L

Reputation: 2314

Please try this

<?php
global $post;
$args = array( 'posts_per_page' => -1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : 
  setup_postdata( $post ); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach;
wp_reset_postdata(); ?>

Upvotes: 2

l&#39;L&#39;l
l&#39;L&#39;l

Reputation: 47284

You must pass a reference to the global $post variable, otherwise functions like the_title() don't work properly. So above $args:

global $post;
$args = array( 'numberposts' => -1); 
$posts= get_posts( $args );
if ($posts) {
    foreach ( $posts as $post ) {
        setup_postdata($post);
        the_title();
    }
}
wp_reset_postdata()

Also, use wp_reset_postdata() to restore globals to the original state when complete.

More Information

Upvotes: 2

Related Questions