简体   繁体   English

从Wordpress获取所有帖子作为JSON

[英]Get all posts from Wordpress as JSON

I am trying ti get all the posts form my Wordpress website with the post_type "product". 我正在尝试通过post_type“product”获取我的Wordpress网站上的所有帖子。

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. 虽然当我将'posts_per_page' => 450添加到$args ,它会返回该类型的帖子,但是如果我添加一个高于450的值(如500),它将不再返回任何内容。

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. 如何修复$args以获取所有产品帖子。

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 ); 或者你可以省略foreach并且只是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. 你可以直接在phpmyadmin中运行这段代码。 and see if any post is there. 并查看是否有任何帖子。 and please replace $wpdb with the prefix of you tables. 请将$ wpdb替换为表格的前缀。

<?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"}... 输出:[{“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: 这是您在查询中显示所有帖子的方式: 'posts_per_page' => -1 - 因此您的查询变为:

$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. 根据我的经验, 'posts_per_page' => -1将不会返回所有帖子,我不知道为什么。 Somehow wordpress not returning more than 500 or 1000 posts from database... 不知何故wordpress没有从数据库中返回超过5001000个帖子...

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. 这个列在WordPress.org网站上,似乎它可能会对你有所帮助。

JSON API JSON API

It's always best to check the WordPress.org hosted plugins first 最好先检查WordPress.org托管的插件

$args = array( 'posts_per_page' => -1, 'post_type' => 'product' ); $ args = array('posts_per_page'=> -1,'post_type'=>'product');

$myposts = get_posts( $args ); $ myposts = get_posts($ args);

echo json_encode($myposts); echo json_encode($ myposts);

I think this should work 我认为这应该有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM