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
these codes work print mysql table datas into my admin menu page. thanks for
for your reply.
}
Recommend using
WP_Query
and getter functions (rather than print functions), so you don’t have to usesetup_postdata()
(untested):