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

Php 以JSON格式从Wordpress获取所有帖子,php,wordpress,Php,Wordpress,我正在尝试用post_类型的产品从我的Wordpress网站获取所有帖子 我试过下面的方法,但不起作用 <?php $args = array( 'post_type' => 'product', 'post_status' => 'publish'); $loop = new WP_Query( $args ); $array = array(); while ( $loop->have_posts() ) : $loop->the_post();

我正在尝试用post_类型的产品从我的Wordpress网站获取所有帖子

我试过下面的方法,但不起作用

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

这就是在查询中显示所有帖子的方式:“每页帖子数”=>-1-因此您的查询变成:

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

据我所知,你需要手动查询帖子

就这样,

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 );
根据我的经验,'posts\u per\u page'=>-1不会返回所有帖子,我不知道为什么。
不知何故,wordpress从数据库返回的帖子不超过500或1000篇…

您可以尝试使用为此目的开发的插件。这是WordPress.org网站上列出的,它似乎可以帮助你


最好先检查WordPress.org托管的插件,无需在此模拟循环。这是最直接的方法:

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

或者您可以省去foreach,只回显json_encode$posts;获取帖子的所有属性。

$args=array'posts\u per\u page'=>-1,'post\u type'=>'product'

$myposts=get_posts$args

echo json_编码$myposts


我想这应该行

请检查您的表格中的数据。是否有任何职位的职位类型的产品。如果是,那么简单的查询应该可以工作

SELECT * FROM $wpdb->posts WHERE $wpdb->posts.`post_type` = 'product' AND $wpdb->posts.`post_status` = 'publish'
您可以直接在phpmyadmin中运行此代码。看看那里有没有帖子。请将$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();
?>   
输出:

[{ID:1,post_title:Hello world!},{ID:63,post_title:Blockquote post}…

你应该包括一些关于阿法比·亚历克斯的更多细节/解释:什么是OP?。我的意思是解释区别-OP的=>-1部分主要是原始海报-1代表WordPress中的所有帖子-没什么了。@Banago我试过$args=array'post\u type'=>'product','posts\u per\u page'=>-1;,但它在页面上没有返回任何内容。Bl虽然有点零零主题,但您会使用类似于您的作品吗?您是否通过调用register\u post\u type创建了自定义的post类型产品?如果是,您可以发布该代码吗?如果您删除全局$product行,您是否可以查看是否返回了任何产品帖子?您是否打算通过ajax使用此产品?@eddiemoya不,我不是。我只是想拥有它页面上的产品数据为json,我从外部c应用程序的url读取数据。因此,我需要从这个查询中返回所有产品,并将其数据显示为json。我尝试过:不是这样的…只需打印$loop…您将从$$wpdb->get_results获得结果数组。我还尝试过它返回空方括号ts.So empty againI尝试了$args=array'post\u type'=>'product','post\u status'=>'publish','nopaging'=>true',posts\u per\u page'=>-1;但这返回的是空的。我在数组中没有返回任何产品数据。尝试删除每页的posts\u。毕竟,你要求的是不分页,对吧?尝试过了。它也返回空的。没有数据。我想一定有这里有些混乱,因为我得到了完美的结果。我已经更新了我的答案,给出了一个完整的脚本。尝试一下。只是一个旁注:在你的foreach循环中,ID需要大写为$post->ID。我尝试从wp_posts中选择*,其中wp_posts.post_在phpmyadmin中键入“=”product”和wp_posts.post_status=”publish',它正确地获取了数据。H尽管我尝试了php代码,但它没有返回任何结果。请查看我上面问题中的代码。全局$wpdb;$args=SELECT*FROM$wpdb->posts其中$wpdb->posts.post类型='product'和$wpdb->posts.post状态='publish'ORDER BY$wpdb->posts.ID;$loop=$wpdb->get_results$args;echo;并告诉我$wpdb对象的输出,以便我可以看到w这是错误的,我在上面的问题中发布了一个屏幕截图,请参见。以防万一,我复制了一些返回的文本。请参见上面的内容。因此,当我尝试此操作时,我对返回的一些数据进行了编码。那么问题出在哪里?在构建循环以将产品作为结构化json时?不确定如何修复此问题。它工作正常,并且ata就要来了。我可以看到。现在让我给你代码来循环产品。当我尝试这个时,得到了相同的结果,它返回空,没有返回任何内容。和以前一样,如果我将-1改为470以下的数字,那么它就工作了,我得到了结果。尝试像这样使用-1'-1'如何获得缩略图和自定义帖子元?这个插件被删除。
<?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();
?>