skip to Main Content

I’m having a bit of a problem in WordPress. I’m looping through a set of data and creating new posts. I need a way to only create the post if its already not exists.

I tried this, but figured it was a quite stupid idea. But can i do something similar? I cant check the titles, since they in many cases will be the same. Can i do the check on import_id somehow? If i do $post_id->import_id its undefined..

Current code:

 for($i = 0; $i < count($postdata); $i++) { //array of data.

    $id = rand(1000, 9999);

    if(is_null(get_post($id))) {
      $post_id = wp_insert_post(array(
        'import_id' => $id, //number
        'post_status' => $post_status, //string
        'post_type' => $post_type, //string
        'post_title' => $post_title, //string
        'post_content' => $post_content //string
      ));
    }
  }

2

Answers


  1. You can use the get_post_status() function to tell if a post exists. It will return false if the post does not exist:

    if ( get_post_status( $id ) ) {
        // do stuff
    }
    

    With your example:

    for($i = 0; $i < count($postdata); $i++) { //array of data.
    
        $id = rand(1000, 9999);
    
        if ( get_post_status($id) ) {
            $post_id = wp_insert_post(array(
              'import_id' => $id, //number
              'post_status' => $post_status, //string
              'post_type' => $post_type, //string
              'post_title' => $post_title, //string
              'post_content' => $post_content //string
            ));
        }
    }
    
    Login or Signup to reply.
  2. You can use post_exists() it will check based on title. check the code below.

    for($i = 0; $i < count($postdata); $i++) { //array of data.
    
        $id         = rand(1000, 9999);
        $fount_post = post_exists( $post_title );
    
        if( !$fount_post ) {
    
            $post_id = wp_insert_post( array(
                'import_id'    => $id, //number
                'post_status'  => $post_status, //string
                'post_type'    => $post_type, //string
                'post_title'   => $post_title, //string
                'post_content' => $post_content //string
            ) );
    
        }
    
    }
    

    Update as per OP code.

    $events       = json_decode($res,true);
    $this->events = $events['Result'];  
    $postdata     = array();
    $index        = 0;
    
    foreach($this->events as $key=>$event) {
        $post_status = 'publish';
        $post_type = EVENTS_CALENDAR_POST_TYPE;
        $post_title = $event['Heading'];
        $post_content = $event['Description'];
        $post_image = $this->media_url . $event['ImageLarge']['Path'];
        $post_thumb = $this->media_url . $event['Image']['Path'];
        $post_event_id = $event['Id'];
    
        foreach($this->events[$key]['Dates'] as $date) {
            $post_date = substr($date['Date'], 0, 10);
            $post_arena = $date['Arena']['Name'];
            $postdata[$index]['id'] = $index;
            $postdata[$index]['status'] = $post_status;
            $postdata[$index]['posttype'] = $post_type;
            $postdata[$index]['date'] = $post_date;
            $postdata[$index]['arena'] = $post_arena;
            $postdata[$index]['heading'] = $post_title;
            $postdata[$index]['desc'] = $post_content;
            $postdata[$index]['image'] = $post_image;
            $postdata[$index]['thumb'] = $post_thumb;
            $index++;
        }
    }
    
    for($i = 0; $i < count($postdata); $i++) { //array of data.
    
        $id = rand(1000, 9999);
        
        $args = array(
           'meta_key' => 'post_event_id'
           'meta_query' => array(
               array(
                   'key' => 'post_event_id',
                   'value' => $post_event_id,
                   'compare' => '=',
               )
           )
        );
    
        $event = new WP_Query( $args );
    
        if( !$event->have_posts() ) {
    
            $post_id = wp_insert_post( array(
                'import_id'    => $id, //number
                'post_status'  => $post_status, //string
                'post_type'    => $post_type, //string
                'post_title'   => $post_title, //string
                'post_content' => $post_content, //string
                'meta_input'   => array(
                    'post_event_id' => $post_event_id,
                )
            ) );
    
        }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search