WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a script that loops through some data and automatically creates some posts.

foreach($data['title'] as $title)
{
    $my_post = array(
     'post_title' => $title,
     'post_content' => $data['descr']["{$title}"],
     'post_status' => 'publish',
     'post_author' => 1,
     'post_category' => array(7)
    );

    // Insert the post into the database
    $post_id = wp_insert_post( $my_post );
    add_post_meta($post_id, 'web_link', $data['link']["{$title}"]);
}

After the script is run I got to posts in the backend and see them listed as published. I then go to the homepage (my posts list page) but there are none listed. I go back to the backend to see they have all been moved to trash. I try to restore them all but the same thing just keeps happening.

Any ideas?

share|improve this question
    
How is this script triggered? It sounds like something runs when you visit the front end and is causing the posted to be deleted. Does this always occur just after you visit the front end? – s_ha_dum Nov 11 '12 at 23:51
    
The code I listed is in a php file which is loaded into functions.php via require_once(). Eventually it'll be a cron job but at the moment I am loading the file, accessing the backend to trigger it, then I comment it out, and then visit the front-end to see the posts. Yes I can reload the posts lists in the backend and they remaing pushlished until I visit the homepage. – iamjonesy Nov 12 '12 at 11:00
    
Disable all plugins and test again. – TheDeadMedic Nov 12 '12 at 12:10
    
Nothing in that particular piece of code should be deleting your posts, even if you didn't comment it out. (You'd get duplicates). Do what TheDeadMedic said. – s_ha_dum Nov 12 '12 at 14:17
    
I'll try disabling all plugins when I get back. It's a bit trickier as it's a WP theme I bought off of themeforrest so it's been pretty modified. – iamjonesy Nov 12 '12 at 16:17

@iamjonesy, One possibility is you your theme already has some deleted posts with similar title or slugs or theme provider has modified the hook with some filter.

Here is a few things you can do to check: Try to find the page with title If it’s in the trash, update it & change it’s status; Otherwise, programmatically create it.

foreach($data['title'] as $title)
{

// Initialize the page ID. This indicates no action has been taken.
    $page_id = -1;

// First, try to get the page
$page = get_page_by_title( $title, OBJECT, 'page' );

// If the page doesn't exist, create it
if( null == $page ) {

Otherwise, if the page is in the trash, update it and change its status to publish

} elseif( 'trash' == strtolower( $page->post_status ) ) {

    $page->post_status = 'publish';
    -----your code ---------

Hope this help :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.