最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - wp_insert_post generates endless posts

programmeradmin0浏览0评论

I'm using wp_insert_posts to programmatically create a post but it keeps generating the same post endlessly. I'm using a custom post type called irl-today. It does generate one post if I'm using the page custom post type but does an endless generate of the post on the 'irl-today` custom post type.

Is there a way to generate a post only once? This is what I have in my functions.php:

<?php
if ( !is_admin() ) { /* Main site*/
} else { /* Admin */

if( ! get_page_by_title('Archive') ) {
$my_post  = array( 'post_title'     => 'Archive',
           'import_id'      => '1041',
           'post_type'      => 'irl-today',
           'post_name'      => 'archives',
           'post_content'   => 'All of DailyBayou publications.',
           'post_status'    => 'publish',
           'comment_status' => 'closed',
           'ping_status'    => 'closed',
           'post_author'    => 1,
           'menu_order'     => 0,
           'guid'           => site_url() . '/archives' );
$PageID = wp_insert_post( $my_post, FALSE ); // Get Post ID - FALSE to return 0 instead of wp_error.
}
};?>

It does generate infinity posts even if it's outside the if { } else { };

I'm using wp_insert_posts to programmatically create a post but it keeps generating the same post endlessly. I'm using a custom post type called irl-today. It does generate one post if I'm using the page custom post type but does an endless generate of the post on the 'irl-today` custom post type.

Is there a way to generate a post only once? This is what I have in my functions.php:

<?php
if ( !is_admin() ) { /* Main site*/
} else { /* Admin */

if( ! get_page_by_title('Archive') ) {
$my_post  = array( 'post_title'     => 'Archive',
           'import_id'      => '1041',
           'post_type'      => 'irl-today',
           'post_name'      => 'archives',
           'post_content'   => 'All of DailyBayou publications.',
           'post_status'    => 'publish',
           'comment_status' => 'closed',
           'ping_status'    => 'closed',
           'post_author'    => 1,
           'menu_order'     => 0,
           'guid'           => site_url() . '/archives' );
$PageID = wp_insert_post( $my_post, FALSE ); // Get Post ID - FALSE to return 0 instead of wp_error.
}
};?>

It does generate infinity posts even if it's outside the if { } else { };

Share Improve this question asked Jan 30, 2020 at 23:36 OpenBayouOpenBayou 155 bronze badges 2
  • 2 functions.php is run every time a page on your site is loaded, so a post will be created each time. When is the post supposed to be created? When the theme is activated? – Jacob Peattie Commented Jan 30, 2020 at 23:42
  • Ideally, create it once when logged into the site. It’s mainly in the event that post is deleted and it auto generates the post. There’s a way to run a pho function once? – OpenBayou Commented Jan 31, 2020 at 1:52
Add a comment  | 

1 Answer 1

Reset to default 1

If you want to create a particular page once, then you can save an option to the database once the page has been created, then check if that value exists before attempting to recreate it. If you store the page's ID, you can also check if the post still exists and recreate it if it doesn't.

add_action(
    'admin_init',
    function() {
        // Check if there's a record of the page.
        $archives_page_id = get_option( 'dailybayou_archives_page_id' );

        // If the page hasn't been created yet, or doesn't exist anymore...
        if ( ! $archives_page_id || ! get_post( $archives_page_id ) ) {
            // ...create the page.
            $archives_page_id = wp_insert_post( 
                [
                    'post_title'     => 'Archive',
                    'import_id'      => '1041',
                    'post_type'      => 'irl-today',
                    'post_name'      => 'archives',
                    'post_content'   => 'All of DailyBayou publications.',
                    'post_status'    => 'publish',
                    'comment_status' => 'closed',
                    'ping_status'    => 'closed',
                ], 
                false
            );

            // Save a record of the page.
            update_option( 'dailybayou_archives_page_id', $archives_page_id );
        }
    }
);
发布评论

评论列表(0)

  1. 暂无评论