programing

"wp_insert_post"를 실행하기 전에 게시물의 제목이 존재하는지 확인하여 게시물의 중복을 방지하는 Word press 방법

jooyons 2023. 3. 26. 11:14
반응형

"wp_insert_post"를 실행하기 전에 게시물의 제목이 존재하는지 확인하여 게시물의 중복을 방지하는 Word press 방법

비누 서버에 접속하는 워드프레스 사이트가 있습니다.문제는 스크립트를 실행할 때마다wp_insert_post또 같은 결과를 사용하고 있습니다.

존재하는지 확인하고 싶습니다.post_title값과 일치합니다.$title일치할 경우,wp_insert_post같은 값을 다시 사용할 수 없습니다.

코드는 다음과 같습니다.

try {
    $client = new SoapClient($wsdl, array('login' => $username, 'password' => $password));
    } catch(Exception $e) {
      die('Couldn\'t establish connection to weblink service.');
    }
$publications = $client->GetPublicationSummaries();
foreach ($publications->GetPublicationSummariesResult->PublicationSummaries->PublicationSummary as $publication_summary) {

    // get the complete publication from the webservice
    $publication = $client->getPublication(array('PublicationId' => $publication_summary->ID))->GetPublicationResult->Publication;
    
    // get all properties and put them in an array
    $properties = array();
    foreach ($publication->Property as $attribute => $value) {
        $properties[$attribute] = $value;
    }
    
    // Assemble basic title from properties
    $title = $properties['Address']->Street . ' ' . $properties['Address']->HouseNumber . $properties['Address']->HouseNumberExtension . ', ' . $properties['Address']->City->_;
}

$my_post = array(
    'post_title'=>$title,
    'post_content'=>'my contents',
    'post_status'=>'draft',
    'post_type'=>'skarabeepublication',
    'post_author'=>1,
);
wp_insert_post($my_post);

도와주셔서 감사합니다.

사용할 수 있습니다.get_page_by_title()커스텀 투고 타입을 서포트하고 있습니다.

if (!get_page_by_title($title, OBJECT, 'skarabeepublication')) :

    $my_post = array(
        'post_title'=>$title,
        'post_content'=>'my contents',
        'post_status'=>'draft',
        'post_type'=>'skarabeepublication',
        'post_author'=>1,
    );
    wp_insert_post($my_post);

endif;

Codex 정보는 이쪽

에 대해 언급하지 않아 놀랐습니다.post_existswp-buff/post.buff로 기능합니다.wpseek의 엔트리를 참조해 주세요.고문서에 기재가 없다.가장 심플하게는get_page_by_title그러나 오브젝트(또는 늘) 대신 포스트 ID(또는 찾을 수 없는 경우 0)를 반환합니다.

$post_id = post_exists( $my_title );
if (!$post_id) {
    // code here
}

답장이 늦어서 죄송합니다.나는 로봇이 말하는 것을 사용했고 이것으로 나의 고민은 해결되었다.감사해요.

$post_if = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_title like '$title_from_soap'");
if($post_if < 1){
    //code here
}

샘플러:

if( !get_page_by_path('mypageslug',OBJECT,'post') ){
  //your codes
}

언급URL : https://stackoverflow.com/questions/7652294/wordpress-how-to-prevent-duplicate-post-by-checking-if-post-title-exist-before-r

반응형