1

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);
}
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.org/Class_Reference/WP_Query

3
  • how can I echo each post title and post? I'm trying to do this foreach ($result->post as $post) { echo $post->post_title; } and reading the docs but I cannot figure it out. Thanks – Lisandro Vaccaro Mar 10 '14 at 14:51
  • @LisandroVaccaro : see the edit. – s_ha_dum Mar 10 '14 at 15:15
  • I just added a new question that steams from this one, you can check it here wordpress.stackexchange.com/questions/137474/… – Lisandro Vaccaro Mar 10 '14 at 15:39
0

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);
}
1
  • 1
    Please add an explanation to your answer: why could that solve the problem? – fuxia Mar 10 '14 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.