Check if post exists

时间:2016-12-18 作者:kilogram

如何检查是否存在名为Weather的帖子?如果不是,我想创建它。

function such_post_exists($title) {
global $wpdb;

$p_title = wp_unslash( sanitize_post_field( \'post_title\', $title, 0, \'db\' ) );

if ( !empty ( $title ) ) {
    return (int) $wpdb->query("SELECT FROM $wpdb->posts WHERE post_title = $p_title");
}

return 0;
}

2 个回复
最合适的回答,由SO网友:Tunji 整理而成

Wordpress有一个post_exists 返回post ID或0的函数。所以你可以

if ( 0 === post_exists( $title ) ) {
    **YOUR_CODE_HERE**
}

SO网友:samjco

如果帖子不存在,请创建它。。。如果确实存在,请更新它。


$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;

相关推荐

浏览器刷新时删除数据库条目,AJAX PHP jQuery

我有一个表单,在通过ajax提交表单时更新数据库表中的列。一切都很好,数据库表列可以获取信息,但一旦刷新浏览器,信息就会从数据库中删除。如果meta\\u值不存在,但meta\\u值也在提交表单时创建的数据库中,则PHP将执行数据库更新。我希望信息保留在数据库中,直到或除非meta\\u值被删除或不存在。任何见解都将不胜感激。PHPadd_action(\'wp_ajax_hide_this\', \'hide_this_by_id\'); add_action(\'wp_ajax_nopriv_