2014-11-13 2 views
0

WP 용 플러그인 개발을 처음 접했습니다. 플러그인 파일의 wp_insert_post에 문제가 있습니다.이 플러그인 PHP 파일을 실행하고 wp_insert_post를 실행하기 전에 모든 코드를 테스트했지만 wp_insert_post에서 점프하면 'Test15'에서 멈추었습니다. 테마 폴더 내의 테스트 페이지에서 코드의 삽입 포스트 부분을 테스트 해 보았습니다. 여기에 어떤 문제가 있는지 확실하지 않습니다. 아래 코드를 참조하십시오.플러그인 개발 - wp 삽입 게시물이 작동하지 않습니다.

function db_importer_insert_properties($properties, $category, $user_id, $post_type) { 

echo 'Test9<br>'; 
$result = true; 
echo 'Test10<br>'; 
if($properties) { 
     echo 'Test11<br>'; 
if(count($properties) > 0) { 
     echo 'Test12<br/>'; 
     print_r($properties); 
     echo '<br/>'; 
    $count = 0; 
    foreach($properties as $property) { 
    echo 'Test13<br/>'; 

    $address =$property['address'] ; //get title 

    $building=$address['streetNumber']; 
    $street=$address['street']; 
    $suburb=$address['suburb']; 
    $state=$address['state']; 
    $postcode=$address['postcode']; 

    $title=$building. ' ' .$street.', '.$suburb.', '.$postcode.', '.strtoupper($state); 

    //test post 
    $new_post = array(
     'post_title' => 'My post10 ', 
     'post_content' => 'This is my post10 ', 
     'post_status' => 'publish', 
     'post_author' => 1, 
     'post_category' => 'uncategorized' 
); 
    echo 'Test14<br/>'; 
    print_r($new_post); 
    echo '<br/>'; 
    echo 'Test15<br/>'; 


    wp_insert_post($new_post); 
    echo 'Test16<br>'; 

    if($post_id != 0) { 

    add_post_meta($post_id, "_bathrooms", esc_attr($property['features']['bathrooms'])); 
    add_post_meta($post_id, "_bedrooms", esc_attr($property['features']['bedrooms'])); 

    if(is_array($property['images'])) { 
     add_post_meta($post_id, "_images", esc_attr(implode("\n", $property['images']))); 
    } 
    else { 
    feedback("Post ID was 0"); 
    } 

    feedback("added property $title with post_id $post_id"); 
    $count++; 
    } 
    else { 
    feedback("post was failed to add"); 
    } 
} 
feedback("Added $count properties"); 

} 
else { 
    feedback("No properties to add."); 
} 
} 
else { 
    feedback("No properties were selected"); 
    $result = false; 
} 
    return $result; 

}

답변

0

당신은 당신의 $post_id 변수를 선언하는 것을 잊었다. 다음을 사용하십시오 :

$post_id = wp_insert_post($new_post, true); 

이 경우 성공시 게시물 ID 또는 실패시 WP 오류가 반환됩니다.

print_r($post_id)을 사용하면 결과를 확인할 수 있습니다.

+0

감사합니다. Diggy,하지만 어떤 이유로 작동하지 않았습니다 ... wp_insert_post 뒤에 어떤 일이 일어나는지 확인하는 방법이 있습니까? –

+0

실패 할 경우 WP 오류 객체를 반환해야합니다 (SHOULD). 내가 알게 된 점은'post_category'는 고양이 ID의 배열을 필요로한다는 것이지만, 큰 문제를 일으킬 것이라고는 생각하지 않습니다. 코드가 포함 된 파일이 WP 기능에 액세스 할 수 있습니까? – diggy

+0

내가 그것을 어떻게 놓쳤는가 ??? hahah, thanks mate___))) /wp-load.php....silly 나를 포함하는 것을 잊었다. .. 다시 고마워, 나는 지난 2 시간 동안 고투하고 있었다 –

관련 문제