我想按标题从PHP中删除一篇文章。
如果帖子按标题存在,我有一个发布或更新的代码,但我不知道如何插入wp_delete_post() function.
这是我发布或更新帖子的实际代码(如果帖子按标题存在):
<?php
// require wp-load.php to use built-in WordPress functions
require_once("/var/www/mysite.com/wp-load.php");
//Title
$TítleProduct = ("Title Product 1");
//Desc
$Desc = ("Product descripction 1");
// Register Post Data
$post = array();
$post[\'post_status\'] = \'publish\';
$post[\'post_type\'] = \'post\'; // can be a CPT too
$post[\'post_title\'] = "$TítleProduct";
$post[\'post_content\'] = ($Desc);
$post[\'post_author\'] = 1;
function wp_exist_post_by_title($title)
{
global $wpdb;
$return_id = $wpdb->get_row("SELECT ID FROM wp_posts WHERE post_title = \'" . $title . "\' && post_type = \'post\' ", \'ARRAY_N\');
if (empty($return_id)) {
return false;
} else {
return $return_id;
}
}
$id = wp_exist_post_by_title($post[\'post_title\']);
if($id !== false)
{
$post[\'ID\'] = $id["0"];
$post_id = wp_update_post( $post );
} else {
$post[\'post_date\'] = $my_post[\'post_date\'];
$post_id = wp_insert_post( $post );
}
/*******************************************************
** SIMPLE ERROR CHECKING
*******************************************************/
$finaltext = \'\';
if($post_id){
$finaltext .= \'Se ha creado/actualizado correctamente el post \'.$post[\'ID\'].\'.<br>\';
} else{
$finaltext .= \'Something went wrong and I didn\\\'t insert a new post.<br>\';
}
echo $finaltext;
?>
我试着替换
wp_update_post 对于
wp_delete_post 并结合
wp_delete_post function 但我不知道如何将其结合起来,下面是wp\\u delete\\u post函数代码示例:
function wp_delete_post( $postid = 0, $force_delete = false ) {
global $wpdb;
$post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid ) );
if ( ! $post ) {
return $post;
}
$post = get_post( $post );
if ( ! $force_delete && ( \'post\' === $post->post_type || \'page\' === $post->post_type ) && \'trash\' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) {
return wp_trash_post( $postid );
}
if ( \'attachment\' === $post->post_type ) {
return wp_delete_attachment( $postid, $force_delete );
}
/**
* Filters whether a post deletion should take place.
*
* @since 4.4.0
*
* @param bool|null $delete Whether to go forward with deletion.
* @param WP_Post $post Post object.
* @param bool $force_delete Whether to bypass the Trash.
*/
$check = apply_filters( \'pre_delete_post\', null, $post, $force_delete );
if ( null !== $check ) {
return $check;
}
/**
* Fires before a post is deleted, at the start of wp_delete_post().
*
* @since 3.2.0
* @since 5.5.0 Added the `$post` parameter.
*
* @see wp_delete_post()
*
* @param int $postid Post ID.
* @param WP_Post $post Post object.
*/
do_action( \'before_delete_post\', $postid, $post );
delete_post_meta( $postid, \'_wp_trash_meta_status\' );
delete_post_meta( $postid, \'_wp_trash_meta_time\' );
wp_delete_object_term_relationships( $postid, get_object_taxonomies( $post->post_type ) );
$parent_data = array( \'post_parent\' => $post->post_parent );
$parent_where = array( \'post_parent\' => $postid );
if ( is_post_type_hierarchical( $post->post_type ) ) {
// Point children of this page to its parent, also clean the cache of affected children.
$children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type );
$children = $wpdb->get_results( $children_query );
if ( $children ) {
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( \'post_type\' => $post->post_type ) );
}
}
// Do raw query. wp_get_post_revisions() is filtered.
$revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = \'revision\'", $postid ) );
// Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
foreach ( $revision_ids as $revision_id ) {
wp_delete_post_revision( $revision_id );
}
// Point all attachments to this post up one level.
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( \'post_type\' => \'attachment\' ) );
wp_defer_comment_counting( true );
$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ) );
foreach ( $comment_ids as $comment_id ) {
wp_delete_comment( $comment_id, true );
}
wp_defer_comment_counting( false );
$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ) );
foreach ( $post_meta_ids as $mid ) {
delete_metadata_by_mid( \'post\', $mid );
}
/**
* Fires immediately before a post is deleted from the database.
*
* @since 1.2.0
* @since 5.5.0 Added the `$post` parameter.
*
* @param int $postid Post ID.
* @param WP_Post $post Post object.
*/
do_action( \'delete_post\', $postid, $post );
$result = $wpdb->delete( $wpdb->posts, array( \'ID\' => $postid ) );
if ( ! $result ) {
return false;
}
/**
* Fires immediately after a post is deleted from the database.
*
* @since 2.2.0
* @since 5.5.0 Added the `$post` parameter.
*
* @param int $postid Post ID.
* @param WP_Post $post Post object.
*/
do_action( \'deleted_post\', $postid, $post );
clean_post_cache( $post );
if ( is_post_type_hierarchical( $post->post_type ) && $children ) {
foreach ( $children as $child ) {
clean_post_cache( $child );
}
}
wp_clear_scheduled_hook( \'publish_future_post\', array( $postid ) );
/**
* Fires after a post is deleted, at the conclusion of wp_delete_post().
*
* @since 3.2.0
* @since 5.5.0 Added the `$post` parameter.
*
* @see wp_delete_post()
*
* @param int $postid Post ID.
* @param WP_Post $post Post object.
*/
do_action( \'after_delete_post\', $postid, $post );
return $post;
}