Hi,

Does anyone here kbow how to get the post not the pages in a wordpress dashboar?
This line of code:
$ArrResult = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type = 'page' ");
and for the table I have something like this:
<td><?php echo $sampleResult->post_title ?></td
I thought this would get the posts, but it returns pages content, author, date. What I want to have is the posts not the pages...

Thank you in advance.

Dani AI

Generated

Good catch, — that was the reason was seeing pages. For reliability and future compatibility, prefer the WordPress APIs rather than hand-rolled SQL. WP functions respect table prefixes, core filters, capabilities, and caching, and they let you filter by status, taxonomy, pagination, etc.

A simple, maintainable pattern is to use WP_Query (or get_posts) and then escape output before printing:

$query = new WP_Query(array(
  'post_type'      => 'post',
  'post_status'    => 'publish',
  'posts_per_page' => 50,
));

if ( $query->have_posts() ) {
  while ( $query->have_posts() ) {
    $query->the_post();
    echo esc_html( get_the_title() );
  }
  wp_reset_postdata();
}

If you have a specific reason to use $wpdb, keep queries minimal and safe by using prepared statements and selecting only needed columns rather than “*”:

$sql = $wpdb->prepare(
  "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = %s AND post_status = %s LIMIT %d",
  'post', 'publish', 50
);
$rows = $wpdb->get_results( $sql );

Final notes: make sure to filter on post_status (publish/draft/private) as needed; remember custom post types will have their own post_type values; and always escape output (for example with esc_html) when rendering titles or other fields. See the WordPress docs for details: WP_Query reference, get_posts(), wpdb::prepare(), esc_html().

Recommended Answers

All 2 Replies

Well, you're asking for it to return pages WHERE post_type = 'page'.

ok thank you..I got it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.