WordPress wp-insert-post 函数创建任意 no-title 页面

WordPress wp-insert-post function creating arbitrary no-title pages

我正在使用 wp-insert post 函数从我正在处理的网站动态生成页面。但是,每次我刷新页面以及从我的代码生成的实际页面时,我都得到这些无标题 posts 。

我已经尝试通过在我的页面创建过程中添加一个挂钩来解决这个问题,但这似乎没有任何作用。我想知道如何只创建我需要的页面而不显示这些无标题 posts。

我的代码:


if( !class_exists("PageCreator")) {
    class PageCreator
    {

        public function __construct()
        {
            add_action('init', array($this, 'AddThisPage'));
        }

        public function AddThisPage()
        {
            $dirName = "/zotpull/resources/temp/";
            $filename =  dirname(__DIR__, 2) . $dirName . "useData.txt";
            $theFile = fopen($filename, "r");
            $msg = fread($theFile, filesize($filename));
            fclose($theFile);

            $links = explode("\n", $msg);
            foreach( array_slice($links, 0, count($links) -1) as $item ) {

                $item = str_replace("/","-",$item);
                $str2 = substr($item, 5);


                $page = array(
                    'page_template' => 'datePage.php', //Sets the template for the page.
                    'post_title' => $str2, //The title of your post.
                    'post_status' => 'publish',
                    'post_type' => 'page'
                );

                if ( ! function_exists( 'post_exists' ) ) {
                    require_once( ABSPATH . 'wp-admin/includes/post.php' );
                }
                $page_exists = post_exists($page['post_title']);

                if ($page_exists == 0) {
                    $insert = wp_insert_post($page);
                }
            }
        }
    }
}

尝试在wp_insert_post之后使用wp_update_post。检查下面的代码。

if( !class_exists("PageCreator")) {
class PageCreator{

        public function __construct()
        {
            add_action('init', array($this, 'AddThisPage'));
        }

        public function AddThisPage()
        {
            $dirName = "/zotpull/resources/temp/";
            $filename =  dirname(__DIR__, 2) . $dirName . "useData.txt";
            $theFile = fopen($filename, "r");
            $msg = fread($theFile, filesize($filename));
            fclose($theFile);

            $links = explode("\n", $msg);
            foreach( array_slice($links, 0, count($links) -1) as $item ) {

                $item = str_replace("/","-",$item);
                $str2 = substr($item, 5);


                $page = array(
                    'page_template' => 'datePage.php', //Sets the template for the page.
                    'post_title' => $str2, //The title of your post.
                    'post_status' => 'publish',
                    'post_type' => 'page'
                );

                if ( ! function_exists( 'post_exists' ) ) {
                    require_once( ABSPATH . 'wp-admin/includes/post.php' );
                }
                $page_exists = post_exists($page['post_title']);

                if ($page_exists == 0) {
                    $insert = wp_insert_post($page);

                    $my_post = array(
                        'ID'           => $insert,
                        'post_title'   => $str2
                    );
                     
                    wp_update_post( $my_post );

                }
            }
        }
    }
}