php 以 JSON 格式从 Wordpress 获取所有帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21730572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:24:49  来源:igfitidea点击:

Get all posts from Wordpress as JSON

phpwordpress

提问by Tester

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

我正在尝试使用 post_type“产品”从我的 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' => 450to $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 $argsto 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();
    ?>

采纳答案by Mubbashar

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.

您可以直接在 phpmyadmin 中运行此代码。看看有没有帖子。并将 $wpdb 替换为您表的前缀。

回答by P_Enrique

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 foreachand just echo json_encode( $posts );to get all the properties of the posts.

或者您可以省略foreach和 只是echo json_encode( $posts );为了获取帖子的所有属性。

回答by awariat

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

回答by Banago

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

回答by Aksh?y Paghdar

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' => -1will not returns all the posts, i don't know why. Somehow wordpress not returning more than 500or 1000posts from database...

根据我的经验,'posts_per_page' => -1不会返回所有帖子,我不知道为什么。不知何故,wordpress 没有从数据库返回更多5001000帖子......

回答by gurudeb

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 托管的插件

回答by Touqeer Shafi

$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);

回声 json_encode($myposts);

I think this should work

我认为这应该有效