Weird problem using wp_insert_post() – probably need a WP guru
-
I’ve got this really weird problem when using wp_insert_post and I can’t work out why it is happening. I’ve narrowed it right down and I can recreate this problem. Someone already tested this code and it worked perfectly on their server, I’m getting a different result though on my site and it only happens when a user is logged in.
I’ve recreated this problem on a spare domain I have at:
http://blogfesta.comI’ve installed a fresh installation of WordPress from cpanel, disabled all plugins, using twentysixteen theme (note: same problem occurs on any theme), and set
admin>settings>reading>blog posts to show = 1I created 6 posts through the admin panel
admin>posts>add newand I created several posts programatically using wp_insert_post(). Here is the code added to twentysixteen functions.php
add_action('admin_init', 'hp_stheme_insert_posts'); function hp_stheme_insert_posts() { if( null == get_page_by_title( 'Test Post 1', OBJECT, 'post' ) ) { wp_insert_post( array( 'post_title' => 'Test Post 1', 'post_name' => 'test-post-1', 'post_status' => 'publish', 'post_type' => 'post', 'post_author' => 1 ) ); } if( null == get_page_by_title( 'Test Post 2', OBJECT, 'post' ) ) { wp_insert_post( array( 'post_title' => 'Test Post 2', 'post_name' => 'test-post-2', 'post_status' => 'publish', 'post_type' => 'post', 'post_author' => 1 ) ); } if( null == get_page_by_title( 'Test Post 3', OBJECT, 'post' ) ) { wp_insert_post( array( 'post_title' => 'Test Post 3', 'post_name' => 'test-post-3', 'post_status' => 'publish', 'post_type' => 'post', 'post_author' => 1 ) ); } if( null == get_page_by_title( 'Test Post 4', OBJECT, 'post' ) ) { wp_insert_post( array( 'post_title' => 'Test Post 4', 'post_name' => 'test-post-4', 'post_status' => 'publish', 'post_type' => 'post', 'post_author' => 1 ) ); } if( null == get_page_by_title( 'Test Post 5', OBJECT, 'post' ) ) { wp_insert_post( array( 'post_title' => 'Test Post 5', 'post_name' => 'test-post-5', 'post_status' => 'publish', 'post_type' => 'post', 'post_author' => 1 ) ); } if( null == get_page_by_title( 'Test Post 6', OBJECT, 'post' ) ) { wp_insert_post( array( 'post_title' => 'Test Post 6', 'post_name' => 'test-post-6', 'post_status' => 'publish', 'post_type' => 'post', 'post_author' => 1 ) ); } if( null == get_page_by_title( 'Test Post 7', OBJECT, 'post' ) ) { wp_insert_post( array( 'post_title' => 'Test Post 7', 'post_name' => 'test-post-7', 'post_status' => 'publish', 'post_type' => 'post', 'post_author' => 1 ) ); } }Now on the website I provided above, the homepage will show one post per page. click the next post button and you’ll see every post one by one in descending order as you would expect. However, if you login, it will show all the posts I created in the admin panel
(admin>posts>add new)as normal, but all the posts created with wp_inset_post() are randomised, misses some posts out, and displays some of the same posts again. So when I create posts with wp_inset_post() they are not displaying correctly when logged in.If you’d like to login to see this, here are login credentials with editor privileges:
username: testuser
password: testuserPlease let me know if this happens when you login and what the problem could be. Thanks in advance.
The topic ‘Weird problem using wp_insert_post() – probably need a WP guru’ is closed to new replies.