Querying data from mysql wordpress

I am new in WordPress I want to get all records from wp_posts table which are drafted, but no record found. My query is the follow:

<?php
    global $wpdb;

    $resultsOb = $wpdb->get_results(
                        "SELECT ID,post_title 
                         FROM $wpdb->wp_posts 
                         WHERE post_status = 'draft'
                         "
                    );

    // loop the objects
    foreach($resultsOb AS $result) {
?>
<tr>
    <td><?php echo $result->ID;?></td>
    <td><?php echo $result->post_title;?></td>
</tr>
<?php } ?>

Related posts

Leave a Reply

1 comment

  1. There’s a built-in function for this called get_posts(). In your case, usage would look something like:

    $args = array(
        'post_status' => 'draft',
        'posts_per_page'   => -1,
    );
    $resultsOb = get_posts( $args );