Get all posts from Wordpress as JSON - php

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();
?>

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.

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.

<?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"}...

This is how you show all posts in a query: 'posts_per_page' => -1 - so your query becomes:
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );

As far as i know, you need to get posts with manual query,
Just like this,
global $wpdb;
$args = "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.`post_type` = 'product' AND $wpdb->posts.`post_status` = 'publish' ORDER BY $wpdb->posts.`ID`";
$loop = $wpdb->get_results( $args );
In my experience, 'posts_per_page' => -1 will not returns all the posts, i don't know why.
Somehow wordpress not returning more than 500 or 1000 posts from database...

You might try using a plugin developed for such a purpose. This one is listed on the WordPress.org website, and it seems like it may help you.
JSON API
It's always best to check the WordPress.org hosted plugins first

$args = array( 'posts_per_page' => -1, 'post_type' => 'product' );
$myposts = get_posts( $args );
echo json_encode($myposts);
I think this should work

Related

Wordpress: Show three random posts on every page refresh

In my WordPress, I am able to show one random post by using the below code:
global $post;
if ( post_type_exists( 'testimonial' ) ) {
$testimonial_query = new WP_Query( array(
'post_type' => 'testimonial',
'orderby' => 'rand',
'posts_per_page' => -1
) );
if ( $testimonial_query->have_posts() ) {
$random_int = rand( 0, $testimonial_query->post_count - 1 );
$post = $testimonial_query->posts[$random_int];
setup_postdata( $post );
// do something with post - e.g. the_excerpt(), the_content(), etc.
}
// Restore original post data
wp_reset_postdata();
}
Ref: https://barn2.co.uk/how-to-display-a-random-post-in-wordpress/
Help me to show 3 random posts by using the above method.
Can you try using in array property or WP_Query. A sample code is given belwo
$results = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE `post_type` = 'testimonial' ORDER BY RAND() LIMIT 3" );
$ids = array();
foreach($results as $$result){
$ids[] = $results->ID;
}
$args = array(
'post_type' => array( 'testimonial' ),
'orderby' => 'ASC',
'post__in' => $ids
);
$testimonial_query = new WP_Query( $args );

Why do I have WooCommerce orders, but no posts of type shop_order?

I have some 30 orders in my shop. I am trying to loop over all orders, but I can't retrieve any orders. Here's the code:
$args = array (
'post_type' => 'shop_order',
'posts_per_page' => - 1
);
$loop = new WP_Query($args);
while ($loop->have_posts()) {
// do some work here
}
The loop never runs. I tried printing a count of all post types:
$args = array (
'post_type' => 'any',
'posts_per_page' => - 1
);
$loop = new WP_Query($args);
$types = array();
while ($loop->have_posts()) {
$loop->the_post();
$post_id = get_the_ID();
$type = get_post_type($post_id);
if ($types[$type]) $types[$type]++;
else $types[$type] = 1;
}
foreach ($types as $type => $count) {
echo "{$type}: {$count} ";
}
This is printing product: 30 page: 5 post: 1, i.e. no shop_orders. I guess I'm missing something very obvious, but it's not so obvious to me what that obvious thing is!
UPDATE: I am now retrieving all orders with this code:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1
);
$posts = get_posts($args);
That doesn't answer the question though. But it's a solution.
use 'post_status' => 'wc-processing' or 'post_status' => 'any'

How to get all post from a post type in Wordpress

I try to get all product from database with function get_post_types,but i get an empty array:
<?php
$list=mysql_query("SELECT * FROM wp_posts where post_type='product'");
if (empty($list)) {
echo "NONE";
}
?>
Try with:
$products = get_posts(array(
'numberposts' => -1,
'post_type' => 'product',
));
Check that post_type name is correct.
This query will get all the post with type product, for get only the published ones, add:
'post_status' => 'publish'
To the array arguments.
From a post you can get a lot of information:
To get the post image:
$img = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
To get the category of a post:
$cats = get_the_category( $post->ID );
$cats is an array of terms.
you may use the standard WP_Query method instead:
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$product_query = new WP_Query( $args );
if( $product_query->have_posts() ){
while( $product_query->have_posts() ){
$product_query->the_post();
if( has_post_thumbnail() ){
the_post_thumbnail();
}
}
}

How to get all video posts by specific user id

I need videos posted by any specific user.
I have used the following code but it is still showing all the posts
$args = array ( 'post_author' => $id, 'post_type' => 'videos', 'posts_per_page' => 100,);
$posts_array = get_posts( $args );
You can use the WP_Query like this.
$query = new WP_Query( 'author=' . $id . '&post_type=video&posts_per_page=100');
while($query->have_posts()) {
the_post();
// Do you magic here
}

How to get posts for specific categories in Wordpress?

I am wondering if there is a function in Wordpress where I can get all the posts in specific cateogires and order them according to category so each category would have an array of posts that belong to it.
I tried to us get_posts function but with no luck:
$args = array(
'numberposts' => 15,
'category' => '161,165,166,1',
);
$postslist = get_posts($args);
You should put ID of WP post and with functon get_post for retrieving post data as array.
<?php
$my_id = 7;
$post_id_7 = get_post($my_id, ARRAY_A);
$title = $post_id_7['post_title'];
?>
Full reference: http://codex.wordpress.org/Function_Reference/get_post
If you want get a posts by category id use:
$post_categories = wp_get_post_categories( $post_id );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}
or dump with full data:
var_dump($cat);
You should use query_posts and the category__in or category__and argument - http://codex.wordpress.org/Function_Reference/query_posts
$args = array( 'category__in' => array(161,165,166,1), 'posts_per_page' => 15 );
query_posts( $args );
while (have_posts()): the_post();
the_title();
endwhile;

Categories