사용자 정의 필드를 사용하여 프로그래밍 방식으로 게시물 (사용자 정의 게시물 유형)을 게시


17

많은 맞춤 입력란이 포함 된 맞춤 게시물 유형 '참가자'가 있습니다. 또한 사용자가 입력 할 수있는 해당 입력 필드가있는 양식이 있습니다. 그가 양식을 제출하면 사용자가 선택한 값을 포함하는 각 사용자 정의 필드로 새 게시물이 생성되기를 원합니다.

그렇게 할 수 있습니까? 그렇다면 어떻게합니까?

답변:


29

다음과 같이 wp_insert_post ()add_post_meta () 를 사용하십시오.

// insert the post and set the category
$post_id = wp_insert_post(array (
    'post_type' => 'your_post_type',
    'post_title' => $your_title,
    'post_content' => $your_content,
    'post_status' => 'publish',
    'comment_status' => 'closed',   // if you prefer
    'ping_status' => 'closed',      // if you prefer
));

if ($post_id) {
    // insert post meta
    add_post_meta($post_id, '_your_custom_1', $custom1);
    add_post_meta($post_id, '_your_custom_2', $custom2);
    add_post_meta($post_id, '_your_custom_3', $custom3);
}

워드 프레스 4.4.2에서도 매력으로 작동합니다 :)!
jave.web

요즘에는 wp_insert_post의 meta_input 키를 통해 메타 파일을 추가 할 수 있습니다.'meta_input' => ['_your_custom_1' => $custom1, '_your_custom_2' => custom2]
Andreas

좋은 지적 @ 안드레아스, 나는 당신이 새로운 답변으로 그것을 추가하고 투표를 시작하자 제안합니다. 이제 답이되어야합니다.
webaware

Thx @webaware :)
Andreas

WordPress 5.1에서도 매력으로 작동합니다 :)!
나는 가장 멍청한 사람이다

6

의 @webaware 에 대한 훌륭한 답변 외에도 wordpress 4.4.0 부터 wp_insert_post 호출을 통해 처리 할 수 ​​있습니다 .

$post_id = wp_insert_post(array (
    'post_content' => $content,
    'post_title' => $title,
    'post_type' => 'your_custom_post_type',
    'post_status' => 'publish',

    // some simple key / value array
    'meta_input' => array(
        'your_custom_key1' => 'your_custom_value1',
        'your_custom_key2' => 'your_custom_value2'
        // and so on ;)
    )
));

if ($post_id) {
    // it worked :)
}

4

이것은 Gravity Forms 플러그인을 사용하여 매우 쉽게 달성 할 수 있습니다 . 백엔드에서 사용자 정의 게시물 유형을 채우는 양식을 작성할 수 있습니다. 이 게시물은 초안 또는 게시 된 것으로 표시되도록 설정할 수 있습니다. 사용자 정의 필드를 추가하는 데 문제가 없습니다. 제 경우에는 고객 평가를 수집하는 데 사용했습니다.


특히 양식 자체를 관리하려는 클라이언트에게 솔루션을 제공하는 경우이 솔루션이 마음에 듭니다.
webaware
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.