2013-10-27 3 views
0

특정 카테고리의 일부 게시물과 함께 json 인쇄를하고 싶습니다.wordpress에서 get_results를 사용하여 메타 데이터 가져 오기

은 내가 키이스 위도경도 (좌표)와 예 : 제목, 카테고리 다음 몇 가지 메타 데이터를합니다.

결과/JSON : [{"meta_key1":"meta_value1","meta_key2":"meta_value2","post_title":"The title","post_cat":"cat_ID")"}

케이스/예 : [{"lat":"55.395263","lon":"10.385723","post_title":"Mammas Pizza","post_cat":"Fast Food")"}

여기

내 시험 중 하나입니다 - 내 쿼리?

$result = $wpdb->get_results("SELECT a.*, b.* FROM $wpdb->posts a LEFT JOIN $wpdb->postmeta b ON b.post_id = a.ID"); 

    if (!$result['error']) { 
     print json_encode($result); 
    } else { 
     errorJson('fail'); 
    } 
+0

Class reference-> WP_Query가이 결과 변수에 저장된 정보입니다. 이렇게하고 결과를 게시하십시오 : console.log ($ result); – Chelseawillrecover

+0

나는 이해하지 못한다. '어떤 정보'에 대해 당신은 무엇을 의미 했습니까? 메타 필드 또는? – user2908800

+0

내 질문을 수정 해보십시오 ... 결과는 "print json_encode ($ result);"에서 얻을 수 있습니다. 성명서? – Chelseawillrecover

답변

0

먼저 우리의 다음 단계는 무엇인가 좋은 배열에 게시 된 카테고리를 가져 오는 함수를 정의하십시오.

function get_post_categories() 
{ 
    $categories = get_the_category(); 
    $cats =array(); $counter=0; 
    foreach ($categories as $cat) 
     $cats[$counter] = $cat->cat_name; 

    return implode(',',$cats); 
} 

그런 다음 좋은 JSON 배열에 게시물과 팩 정보를 가져 오는 함수를 정의합니다. 당신이 $ 인수에 문제가 발생하는 경우

function get_my_json_posts() 
{ 
    $args = array (
     'post_type' => 'post', 
     'post_status' => 'publish' 
    ); 
    $posts = array(); $i= 0; 
    $query = new WP_Query($args); 
    while ($query->have_posts()) : $query->the_post(); 
     $posts[$i++] = array(
      'title' => get_the_title(); 
      'categories' => get_post_categories(); 
      'lat' => get_post_meta('lat'); 
      'lon' => get_post_meta('lon'); 
     ); 
    endwhile; 

    return json_encode($posts); 
} 

여기에 모든 매개 변수를 볼 수 있습니다

+0

흠, 코드에 아무런 결과가 없으므로 먼저 배열에서;을 변경합니다. 그는 워드 프레스 밖에서 (물론 "require ('wp-load.php');") 맨 위에? 카테고리 (예 : 음식)에있는 모든 게시물을 가진 목록을 원하고 거기에있는 모든 게시물이 json '{ "lat": "55.395263", "lon": "10.385723", "post_title": "Mammas Pizza", "post_cat": "패스트 푸드"} "}' – user2908800

+0

잘 될 것입니다. $ args 배열에있는 몇 가지 질의 args가 있어야합니다. –

+0

이것은 지금 제 코드입니다. 잘못된 결과는 없습니다 :' function get_my_json_posts() { $ args = array ('post_type'=> 'post', 'post_status'=> 'publish'); $ posts = array(); \t $ i = $ query = new WP_Query ($ args); while ($ query-> have_posts()) : $ query-> the_post(); $ 소식 [$ I ++] = 배열 ​​( '제목'=> get_the_title() '북'=> get_post_meta ('북') '경도'=> get_post_meta ('경도') ); \t endwhile; return json_encode ($ posts); }' – user2908800

관련 문제