skip to Main Content

I am trying to retrieve data from post table into admin menu page. I have written my codes below. it doesn’t display records from post table.

    function wdbp_menu_page(){
    add_menu_page( 
        __( 'My wpdb', 'wpdb' ),
        'my db',
        'manage_options',
        'customdb',
        'my_menu_page',
        7
    ); 
}

add_action( ‘admin_menu’, ‘wdbp_menu_page’ );

function my_menu_page(){
global $wpdb;
$fivesdrafts = $wpdb->get_results(
    "
        SELECT *
        FROM $wpdb->posts
        WHERE post_status = 'draft'
        AND post_author = 5
    "
);
if ( $fivesdrafts ) {
    foreach ( $fivesdrafts as $post ) {
        setup_postdata( $post );
        ?>
        <h2>
            <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permalink: <?php the_title(); ?>">
                <?php the_title(); ?>
            </a>
        </h2>
        <?php
    }
} else {
echo "Page not fount";

}
}

2

Answers


  1. Chosen as BEST ANSWER

    these codes work print mysql table datas into my admin menu page. thanks for
    for your reply.

     function my_menu_page(){
    
    global $wpdb;
    $fivesdrafts = $wpdb->get_results(
        "
            SELECT *
            FROM slides
            WHERE ID = '1'
        "
    );
    
    $sql_results = $fivesdrafts;
    if ($sql_results) {
        foreach ($sql_results as $post) {
          echo $post->name;
        }
    }
    

    }


  2. Recommend using WP_Query and getter functions (rather than print functions), so you don’t have to use setup_postdata() (untested):

    function my_menu_page() {
        $fivesdrafts = new WP_Query( array(
            'posts_per_page' => -1,
            'post_status'    => 'draft',
            'author'         => 5,
            'no_found_rows'  => true,
        ) );
    
        if ( ! $fivesdrafts->have_posts() ) {
            echo __( 'Page not found' );
        }
    
        foreach ( $fivesdrafts->posts as $post ) {
            $title = get_the_title( $post );
    
            printf( 
                '<h2><a href="%s" rel="bookmark" title="Permalink: %s">%s</a></h2>',
                esc_attr( get_permalink( $post ) ),
                esc_attr( $title ),
                esc_html( $title )
            );
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search