2014-02-12 107 views
9

我正在嘗試使用post_type「product」獲取所有帖子,形成我的Wordpress網站。從Wordpress獲取所有帖子爲JSON

我嘗試了下面,但它不起作用。

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

雖然當我添加'posts_per_page' => 450$args,它返回類型的職位,但是如果我添加值高於450如500,再次回到什麼。

我正在使用它遍歷所有產品帖子,並將名稱,價格等添加到循環中的數組中。

如何修復$args以獲取所有產品帖子。

編輯:

最近我也試過:

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

雖然稍有點兒主題,但會使用類似[JSON API插件](http://wordpress.org/plugins/json-api/)爲您工作? – T0xicCode

+0

您是否通過調用register_post_type()來創建自定義帖子類型「product」?如果是這樣,你可以發佈該代碼嗎? – PhoenixWing156

+0

如果您刪除全球$產品線,您是否可以查看是否有任何產品帖子被退回? – PhoenixWing156

回答

3

請檢查您的表中的數據。有沒有帖子類型的產品。如果是,那麼簡單的查詢應該可以工作

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

您可以直接在phpmyadmin中運行此代碼。看看有沒有帖子。並且請將$ wpdb替換爲表格的前綴。

+0

我試着在phpmyadmin中選擇*從wp_posts WHERE wp_posts.'post_type' ='product'AND wp_posts.'post_status' ='publish''並且它正確地獲取了數據。不過,我嘗試在PHP代碼中,它什麼都沒有返回。請參閱上面我的問題中的代碼。 – Tester

+0

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

';print_r($wpdb);echo '
';並告訴我$ wpdb對象的輸出,所以我可以看到什麼是錯的 – Mubbashar

+0

我在上面的問題中發佈了一個屏幕截圖,請參閱。還有,以防萬一,我還複製了一些作爲文本返回的內容。請參閱上面的 – Tester

1

這是你如何顯示在查詢所有文章:'posts_per_page' => -1 - 讓你的查詢就會變成:

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

你應該爲OP – Alex

+0

Happy Alex提供更多的細節/解釋嗎? :)什麼是OP? – Banago

+0

..我的意思是解釋差異 - (主要是=> -1部分) OP是原創海報 – Alex

1

由於據我所知,您需要通過手動查詢獲得帖子,

就這樣,

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_per_page' => -1不會返回所有的職位,我不知道爲什麼。 不知何故,wordpress不會返回超過5001000來自數據庫的帖子...

+0

我試過了:'<?php 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); $ array = array(); ($ loop-> have_posts()):$ loop-> the_post(); global $ product; (), 'title'=> get_the_title() ); endwhile; wp_reset_query(); ob_clean(); echo json_encode($ array); exit(); ?>' – Tester

+0

這將返回空的 – Tester

+0

它不像那樣.... 只需打印'$ loop' ...您將從'$$ wpdb-> get_results()'獲得結果數組。 –

1

您可能會嘗試使用爲此目的而開發的插件。這個在WordPress.org網站上列出,它似乎可以幫助你。

JSON API

它總是最好檢查WordPress.org舉辦第一

9

沒有必要在這裏模擬迴路插件。這是最簡單的方法:

$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,只是echo json_encode($posts);來獲得職位的所有屬性。

+0

我試過'$ args = array('post_type'=>'product','post_status'=>'publish','nopaging'=> true,'posts_per_page'=> -1);'空。我沒有收到數組中返回的產品數據。 – Tester

+0

嘗試取出'posts_per_page'。畢竟,你要求「不分頁」,對吧? –

+0

試過了。它也返回空。沒有數據。 – Tester

0

的$ args =陣列( 'posts_per_page'=> -1, 'post_type'=> '產品');

$ myposts = get_posts($ args);

echo json_encode($ myposts);

我認爲這應該工作

+0

當我嘗試這個時得到了相同的結果,它返回空,沒有回來。就像以前一樣,如果我將-1更改爲470以下的數字,那麼它就可以工作,並獲得結果。 – Tester

+0

嘗試使用-1就像這樣'-1' –

3
<?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」: 「世界,你好!」},{ 「ID」: 「63」, 「POST_TITLE」: 「大段引用郵報」}。 ..

+0

如何獲取縮略圖和自定義文章元? –

相關問題