WordPress的帖子显示2职位一次与不同的ID在数据库中

9cbw7uwe  于 2023-04-29  发布在  WordPress
关注(0)|答案(1)|浏览(168)

首先感谢大郡
第二声
即时通讯有问题,在相同的职位,它显示2个职位与相同的名称与2个不同的内容和id的职位
我尝试禁用所有插件和更改主题与“二十十九”
刷新后无任何变化
我也检查主题
我用这个sql找到2个帖子我在一个帖子里看到它

SELECT id, post_title, post_status, post_type, SUBSTR(post_content, LOCATE('Carob is an evergreen flowering tree whose', post_content) -1, 100) FROM wpw8_posts WHERE locate('Carob is an evergreen flowering tree whose', post_content) > 0

如何检查此问题我尝试调试,但没有显示为错误或问题页面
我可以附加content-single。PHP和Singlephp,但我已经设置为wordpres的默认主题,所以我不在哪里我可以找到这个问题
“使用autopost插件发布make”

nukf8bse

nukf8bse1#

检查和删除重复的post slug(post_name)的代码可以类似于下面的示例。它使用了WordPress的action hook save_post

警告:在现场/生产现场测试之前,先在开发/测试现场测试所有代码!

此代码尚未测试。默认情况下,WordPress通过在帖子保存到数据库之前自动更改post_name来避免重复的post_names。在你的例子中,你的插件可能正在做一些事情来避免默认的行为。在将其部署到活动站点之前,请在测试站点上对其进行测试。在部署之前,请记住备份您的数据库。

删除重复帖子

function delete_post_with_duplicate_post_name( $post_id, $post, $update ) {
    $force_delete = false;

    // Do not touch updates to existing posts.
    if ( $update ) {
        return;
    }

    $posts = \get_posts( array(
        'post_name__in' => array( $post->post_name ),
        'post_status' => 'publish',
        'numberposts' => 2
    ) );

    if ( 2 === count( $posts ) ) {
        \wp_delete_post( $post_id, $force_delete );
    }
}

add_action( 'save_post', 'delete_post_with_duplicate_post_name', 10, 3 );

$force_delete选项控制帖子是立即发送到垃圾桶还是永久删除。如果被发送到垃圾桶,您有机会看到作者是谁,确定帖子是否应该替换原始帖子,并在需要时恢复它。

搜索重复帖子

搜索重复帖子的SQL查询可能如下所示:

use DATABASE_NAME;

SELECT COUNT(*) AS Count, P.post_name
FROM wp_posts P
WHERE P.post_name <> ''
  AND P.post_status = 'publish'
GROUP BY P.post_name
HAVING Count > 1;

找到重复的帖子后,您可以在管理员中再次查询帖子ID并删除帖子。

更新#2:删除所有重复的帖子

以下代码未经测试

代码应该删除所有基于post_namepost_status的重复帖子。您可以将其添加到插件或子主题的主文件中,使用它来删除重复的帖子,然后在删除重复的帖子后将其删除。
删除帖子的语句已注解掉以供测试。首先,在从该语句中删除注解之前,确认代码找到了正确的帖子。
代码必须从WordPress内部执行,而不是直接在数据库上执行,因为wp_delete_post函数会删除与帖子相关的 meta数据和其他实体。阅读wp_delete_post上的WP文档

function dangerously_delete_duplicate_posts() {
    global $wpdb;
    $force_delete = false;

    $order = 'ASC';

    $table_prefix = $wpdb->prefix;

    // Find duplicate slugs.
    $sql  = "";
    $sql .= "SELECT COUNT(*) AS Count, P.post_name";
    $sql .= " FROM {$table_prefix}posts P";
    $sql .= " WHERE P.post_name <> ''";
    $sql .= " AND P.post_status = 'publish'";
    $sql .= " GROUP BY P.post_name";
    $sql .= " HAVING Count > 1";
    $sql .= ";";

    $post_names = $wpdb->get_col( $sql, 1 );

    if ( count( $post_names ) > 0 ) {

        // Find IDs of duplicates.
        $query = "SELECT DISTINCT P.ID, P.post_name, P.post_date";
        $query .= " FROM {$table_prefix}posts P";
        $query .= ' WHERE P.post_name IN (' . implode(', ', array_fill(0, count( $post_names ), '%s') ) . ')';
        $query .= " AND P.post_status = 'publish'";
        $query .= " ORDER BY P.post_name, P.post_date {$order}";
        $query .= ";";

        $sql2 = call_user_func_array(
            array( $wpdb, 'prepare' ),
            array_merge(
                array( $query ),
                $post_names
            )
        );

        $posts = $wpdb->get_results( $sql2, 'ARRAY_A' );
        $previous_posts = array();
        $found_posts = array();

        // Delete all except the earliest posts for each set of duplicate posts.
        foreach ( $posts as $index => $current ) {
            if ( $index > 0 && $current['post_name'] !== $previous_posts[0]['post_name'] ) {
                foreach ( $previous_posts as $index2 => $previous_post ) {
                    if ( $index2 > 0 ) { // Avoid deleting the first post.
                        /**
                         * Remove the comment from this line when you have
                         * confirmed the correct posts are found.
                         */
                        // \wp_delete_post( $previous_post['ID'], $force_delete );

                        /**
                         * For testing.
                         * Comment out the line below when you have
                         * confirmed the correct posts are found.
                         */
                        $found_posts[] = $previous_post;
                    }
                }
                $previous_posts = array();
            }
            $previous_posts[] = $current;
        }
        foreach ( $previous_posts as $index3 => $previous_post ) {
            if ( $index3 > 0 ) {
                \wp_delete_post( $previous_post['ID'], $force_delete );
            }
        }
    }

    /**
     * For testing.
     * Comment out the line below when you have
     * confirmed the correct posts are found.
     * Instead of echo you could save the result
     * from print_r() to a file and use the file
     * as a reference when verifying the posts in
     * the database or in the WP Admin pages.
     */
    echo print_r( $found_posts, true );
}

add_action( 'init', 'dangerously_delete_duplicate_posts', 10, 1 );

相关问题