Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all posts from Wordpress as JSON

Tags:

php

wordpress

I am trying ti get all the posts form my Wordpress website with the post_type "product".

I tried the below but it doesn't work.

<?php
$args = array( 'post_type' => 'product', 'post_status' => 'publish');

$loop = new WP_Query( $args );

$array = array();

while ( $loop->have_posts() ) : $loop->the_post();

    global $product;
    $array[] = array(
        'id' => get_the_ID(),
        'title' => get_the_title()
    );

endwhile;

wp_reset_query();
ob_clean();
echo json_encode($array);
exit();
?>

Although when I add 'posts_per_page' => 450 to $args, it returns posts of the type, however if I add a value higher than 450 such as 500, it returns nothing again.

I am using this to loop through all product posts and add the name, price etc. to an array in the loop.

How do I fix the $args to get all the product posts.

EDIT:

I also recently tried:

<?php
    $args="SELECT * FROM wp_posts WHERE wp_posts.`post_type` = 'product' AND wp_posts.`post_status` = 'publish'";

    $loop = get_results( $args );

    $array = array();

    while ( $loop->have_posts() ) : $loop->the_post();

        global $product;
        $array[] = array(
            'id' => get_the_ID(),
            'title' => get_the_title()
        );

    endwhile;

   // wp_reset_query();
    ob_clean();
    echo json_encode($array);
    exit();
    ?>
like image 475
Tester Avatar asked Feb 12 '14 14:02

Tester


3 Answers

No need to simulate the loop here. This is the most straightforward way:

$args = array( 
    'post_type' => 'product', 
    'post_status' => 'publish', 
    'nopaging' => true 
);
$query = new WP_Query( $args ); // $query is the WP_Query Object
$posts = $query->get_posts();   // $posts contains the post objects

$output = array();
foreach( $posts as $post ) {    // Pluck the id and title attributes
    $output[] = array( 'id' => $post->ID, 'title' => $post->post_title );
}
echo json_encode( $output );

Or you can leave out the foreach and just echo json_encode( $posts ); to get all the properties of the posts.

like image 184
P_Enrique Avatar answered Oct 06 '22 20:10

P_Enrique


please check your table for data. Is there any post of post type products. if yes then the simple query should work

SELECT * FROM $wpdb->posts WHERE $wpdb->posts.`post_type` = 'product' AND $wpdb->posts.`post_status` = 'publish'

you can run this code in phpmyadmin directly. and see if any post is there. and please replace $wpdb with the prefix of you tables.

like image 45
Mubbashar Avatar answered Oct 06 '22 22:10

Mubbashar


<?php
//this file is in main folder and it works for me(yourWordpressWebsite.com/yourFile.php)

$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';

global $wpdb;
$posts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post'");

echo json_encode($posts);
exit();
?>   

output: [{"ID":"1","post_title":"Hello world!"},{"ID":"63","post_title":"Blockquote Post"}...

like image 32
awariat Avatar answered Oct 06 '22 22:10

awariat