检查帖子标题是否存在,如果不存在,则插入帖子,如果存在,则在Meta中添加增量#

时间:2012-07-15 作者:marctain

我已经有一个功能,用户提交表单并创建自定义帖子。。。

<?php $postTitle = $_POST[\'post_title\'];
$submit = $_POST[\'submit\'];

if(isset($submit)){

    global $user_ID;

    $new_post = array(
        \'post_title\' => $postTitle,
        \'post_content\' => \'\',
        \'post_status\' => \'publish\',
        \'post_date\' => date(\'Y-m-d H:i:s\'),
        \'post_author\' => \'\',
        \'post_type\' => \'stuff\',
        \'post_category\' => array(0)
    );

    $post_id = wp_insert_post($new_post);
add_post_meta($post_id, \'times\', \'1\');

}
我想检查自定义帖子标题是否存在,如果不存在,则继续在元字段中创建带有#1的帖子,如果存在,则只需在元字段中添加1

6 个回复
最合适的回答,由SO网友:Barry Carlyon 整理而成

这需要一个查询。

因此,基于您的代码:

<?php
$postTitle = $_POST[\'post_title\'];
$submit = $_POST[\'submit\'];

if(isset($submit)){

    global $user_ID, $wpdb;

    $query = $wpdb->prepare(
        \'SELECT ID FROM \' . $wpdb->posts . \'
        WHERE post_title = %s
        AND post_type = \\\'stuff\\\'\',
        $postTitle
    );
    $wpdb->query( $query );

    if ( $wpdb->num_rows ) {
        $post_id = $wpdb->get_var( $query );
        $meta = get_post_meta( $post_id, \'times\', TRUE );
        $meta++;
        update_post_meta( $post_id, \'times\', $meta );
    } else {
        $new_post = array(
            \'post_title\' => $postTitle,
            \'post_content\' => \'\',
            \'post_status\' => \'publish\',
            \'post_date\' => date(\'Y-m-d H:i:s\'),
            \'post_author\' => \'\',
            \'post_type\' => \'stuff\',
            \'post_category\' => array(0)
        );

        $post_id = wp_insert_post($new_post);
        add_post_meta($post_id, \'times\', \'1\');
    }
}
应该这样做

SO网友:Tim Hallman

更更新的方法可以使用post_exists() 功能如下:

if( isset( $_POST[\'submit\'] ) ){

   $post_title = sanitize_title( $_POST[\'post_title\'] );

   $new_post = array(
       \'post_title\' => $post_title,
       \'post_content\' => \'\',
       \'post_status\' => \'publish\',
       \'post_date\' => date(\'Y-m-d H:i:s\'),
       \'post_author\' => \'\',
       \'post_type\' => \'stuff\',
       \'post_category\' => array(0)
   );

   $post_id = post_exists( $post_title ) or wp_insert_post( $new_post );

   update_post_meta( $post_id, \'times\', \'1\' );

}

SO网友:alxndrbauer

您可以使用WordPress的get\\u page\\u by\\u title()函数:

<?php $postTitle = $_POST[\'post_title\'];
$submit = $_POST[\'submit\'];


if(isset($submit)){
    $customPost = get_page_by_title($postTitle, OBJECT, \'stuff\');

    if(!is_null($customPost)) {
       $meta = get_post_meta($customPost->ID, \'times\', true);
       $meta++;
       update_post_meta($customPost->ID, \'times\', $meta);

       return
    }

    global $user_ID;

    $new_post = array(
        \'post_title\' => $postTitle,
        \'post_content\' => \'\',
        \'post_status\' => \'publish\',
        \'post_date\' => date(\'Y-m-d H:i:s\'),
        \'post_author\' => \'\',
        \'post_type\' => \'stuff\',
        \'post_category\' => array(0)
    );

    $post_id = wp_insert_post($new_post);
    add_post_meta($post_id, \'times\', \'1\');

}

SO网友:maheshwaghmare

@蒂姆·哈尔曼回答得很好。使用该功能post_exists() 检查post是否存在。更多详细信息请访问https://developer.wordpress.org/reference/

developer doc

SO网友:samjco

You can do it by ID


$post_title = "This Awesome Title";
$post_content = "My content of something cool.";
$post_status = "publish"; //publish, draft, etc
$post_type = "page" // or whatever post type desired

/* Attempt to find post id by post name if it exists */
$found_post_title = get_page_by_title( $post_title, OBJECT, $post_type );
$found_post_id = $found_post_title->ID;

/**********************************************************
** Check If Page does not exist, if true, create a new post 
************************************************************/
if ( FALSE === get_post_status( $found_post_id ) ): 

      $post_args = array(
        \'post_title\' => $post_title,
        \'post_type\' => $post_type,
        \'post_content\'=> $post_content,
        \'post_status\'  => $post_status,
        //\'post_author\'  => get_current_user_id(),

        /* If you have meta fields to enter data into */ 
        \'meta_input\'   => array(
            \'meta_key1\' => \'my value\',
            \'meta_key2\' => \'my other value\',
        ),
      );      


      /* Add a new wp post into db, return it\'s post id */
      $returned_post_id = wp_insert_post( $post_args );  

      /* Update page template only if using "page" as the post_type */ 
      update_post_meta( $returned_post_id, \'_wp_page_template\', \'my-page-template.php\' ); 

      /* Add values into meta fields. Work with ACF CUSTOM FIELDS!! */
      $field_key = "My_Field_KEY";
      $value = "my custom value";
      update_field( $field_key, $value, $returned_post_id );

      $field_key = "My_Other_Field_KEY";
      $value = "my other custom value";
      update_field( $field_key, $value, $returned_post_id );

      /* Save a checkbox or select value */
      // $field_key = "My_Field_KEY";
      // $value = array("red", "blue", "yellow");
      // update_field( $field_key, $value, $returned_post_id );

      /* Save to a repeater field value */
      // $field_key = "My_Field_KEY";
      // $value = array(
      //   array(
      //     "ss_name" => "Foo",
      //     "ss_type" => "Bar"
      //   )
      // );
      // update_field( $field_key, $value, $returned_post_id );

      /* Echo a response! */
      echo "<span class=\'pg-new\'><strong>". $post_title . " Created!</strong></span><br>";
      echo "<a href=\'".esc_url( get_permalink($returned_post_id) )."\' target=\'_Blank\'>". $post_title . "</a><p>";


else:        
/***************************
** IF POST EXISTS, update it 
****************************/

      /* Update post */
      $update_post_args = array(
        \'ID\'           => $found_post_id,
        \'post_title\'   => $post_title,
        \'post_content\' => $post_content,
      );

      /* Update the post into the database */
      wp_update_post( $update_post_args );

      /* Update values into meta fields */
      $field_key = "My_Field_KEY";
      $value = "my custom value";
      update_field( $field_key, $value, $found_post_id );

      $field_key = "My_Other_Field_KEY";
      $value = "my other custom value";
      update_field( $field_key, $value, $found_post_id );

      /* Echo a response! */
      echo "<span class=\'pg-update\'><strong>". $post_title . " Updated!</strong></span><br>"; 
      echo "<a href=\'".esc_url( get_permalink($found_post_id) )."\' target=\'_Blank\'>View</a> | <a href=\'post.php?post=".$found_post_id."&action=edit\'>". $post_title . "</a><p>";

endif;

SO网友:Devendra Sharma

WordPress按标题检查是否存在帖子

function wp_exist_post_by_title( $title ) {
    global $wpdb;
    $return = $wpdb->get_row( "SELECT ID FROM wp_posts WHERE post_title = \'" . $title . "\' && post_status = \'publish\' && post_type = \'post\' ", \'ARRAY_N\' );
    if( empty( $return ) ) {
        return false;
    } else {
        return true;
    }
}

// usage
if( wp_exist_post_by_title( $post->name ) ) {
// post exist
} else { 
// post does not exist
}

结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在