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();
    }
}
?>

Related posts

Leave a Reply

2 comments

  1. 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(); ?>
    
  2. 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