最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wpdb - Get published posts and pages?

programmeradmin0浏览0评论

I'm working on a plugin to use gettext in posts and pages content.

I'm trying to get all published posts and pages in order scan them for translatable strings, but I cannot figure out how to do it.

This is what I'm trying:

$pages = $wpdb->query('SELECT * FROM wp_posts WHERE post_status = "publish"');
foreach ( $pages as $post ) {
    print_r($post);
}

I'm working on a plugin to use gettext in posts and pages content.

I'm trying to get all published posts and pages in order scan them for translatable strings, but I cannot figure out how to do it.

This is what I'm trying:

$pages = $wpdb->query('SELECT * FROM wp_posts WHERE post_status = "publish"');
foreach ( $pages as $post ) {
    print_r($post);
}
Share Improve this question asked Mar 10, 2014 at 13:39 Lisandro VaccaroLisandro Vaccaro 9954 gold badges12 silver badges28 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Don't use pure SQL if you don't have to. WordPress provides a useful and relatively solid class for retrieving post data. Use it.

$args = array(
  'post_type' => array('post','page'),
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'ignore_sticky_posts' => true,
);
$qry = new WP_Query($args);
// Show post titles
foreach ($qry->posts as $p) { 
    echo $p->post_title; 
}

Reference:
http://codex.wordpress/Class_Reference/WP_Query

Try this,

global $wpdb;

$posts = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_status = 'publish'"));

foreach ( $posts as $post ) {
    print_r($post);
}
发布评论

评论列表(0)

  1. 暂无评论