wp_query() function is not showing title

I am working in WordPress and wp_query() function is not bringing posts title from db so please can someone help me??

Here is my code..

Read More
add_action('admin_menu','wpdocs_register_my_custom_menu_page');
/**
* Register a custom menu page.
*/


function wpdocs_register_my_custom_menu_page(){
        add_menu_page( 
            __( 'Admin Pages', 'textdomain' ),
            'Test Menu',
            'manage_options',
            'custompage',
            'my_custom_menu_page'

        ); 
    }

/**
 * Display a custom menu page
 */


function my_custom_menu_page(){
        esc_html_e( 'Admin Pages', 'textdomain' );  
       global $output;
        $pages = new WP_Query(array('post_type'=>'page','posts_per_page'=> '-1','post_status'=>'publish'));
        if($pages->have_posts()){
        while($pages->have_posts()){ ?>

            <h2><?php $pages->the_title() ?></h2>

        <?php   $pages->the_post();
        }
        wp_reset_postdata();
    }
        }

Every effort will be highly appreciated!
Thanks in advance.

Related posts

1 comment

  1. Before using the_title() post must be setup. So use $pages->the_post() before displaying title. So please modify code as

    while($pages->have_posts()){
        $pages->the_posts();
    ?>
         <h2> <?php the_title(); ?> </h2>
     <?php } ?>
    

Comments are closed.