0

I am importing an rss feed from a job listing site and creating a post for each listing i import with wp_insert_post to save the data and display expired listings in an archive later.

I need a solution to keep wp_insert_post from creating duplicats while it still updates the post if certain values have been updated on the job listing from the feed. Each listing has its own ID called AdvertID from the feed.

Just to clearify,"if (!get_page_by_title)" wont do the trick when a lot of job listings can have the same title, so that solution ends up excluding a majority of listings. Everything works fine with the code already there, i just need a solution to the problems i am explaining. Just showing some code so you can get a sense of what i am doing here. `

  $post = array(
    'post_content'   => $item->description,
    'post_date'      => $item_date,
    'post_title'     => $item_title,
    'post_status'    => 'publish',
    'post_type'      => 'jobs',




  );
  $id = wp_insert_post($post);

  $metdata = array(
  'link' => $item->link,
  'date' => $item ->date,
  'company_logo_path' => $item->CompanyLogoPath,
  'company_profile_text' => $item->CompanyProfileText,
0

You can save AdvertID with your post and use it to check if the job was imported before.

/**
 * Gets a job by AdvertId.
 * 
 * @param   string          $advert_id  The AdvertId.
 * @return  WP_Post|bool                The job or <false> if no job is found.
 */
function get_job_by_advert_id( $advert_id ) {
    $args = array(
        'post_type' => 'jobs',
        'meta_query' => array(
            'key' => 'advert_id',
            'value' => $advert_id,  
        ),
    );
    $posts = get_posts( $args );

    if (empty($posts)) {
        return false;
    }

    return $posts[0];
}

// Check if the job was imported before.
$job = get_job_by_advert_id( $item->AdvertId );

if ( $job ) {

    // The job was imported before, update it.
    $args = array(
        'ID' => $job->ID,
        'post_content'   => $item->description,
        'post_date'      => $item_date,
        'post_title'     => $item_title,
    );

    $post_id = wp_update_post( $args ); 

} else {

    // The job is new. Insert it.
    $args = array(
      'post_content'   => $item->description,
      'post_date'      => $item_date,
      'post_title'     => $item_title,
      'post_status'    => 'publish',
      'post_type'      => 'jobs',
    );
    $post_id = wp_insert_post($args);   

    // Save the AdvertId for the next import.
    add_post_meta( $post_id, 'advert_id', $item->AdvertId, true);

}
  • the id function worked great but im not getting my posts updated when there is a change in the job listings from the feed. It just does nothing. – re-boot Oct 25 '16 at 16:44
0

You can eventually set a unique 'post_name' in your wp_insert_post $args. Just create a function to generate it like a password generator will do.

An example:

the function to create the post_name:

function keygen($length=10)
{
    $key = '';
    list($usec, $sec) = explode(' ', microtime());
    mt_srand((float) $sec + ((float) $usec * 100000));

    $inputs = array_merge(range('z','a'),range(0,9),range('A','Z'));

    for($i=0; $i<$length; $i++)
    {
        $key .= $inputs{mt_rand(0,61)};
    }
    return $key;
}

For the wp_insert_post() :

$args = array(
         'post_type' => $cpt_name,
         'post_title'=> $list_name,
         'post_content'  => '',              
         'post_status'   => 'publish',
         'post_name' => keygen(12) 
       );

$post_id = wp_insert_post($args);

Of course, set your own vars and conditions as you want.

Hope it gives you a direction on how to achieve your work.

  • Ye, please give me an example. – re-boot Oct 22 '16 at 9:52
  • ok it's coming ! – Benoti Oct 22 '16 at 9:52
  • Just to clearify, wp inserts the post in a custom post type. And i need wp_insert_post to know that the listing it fetches is already there, if it is, then update values (if the listing is updatet). If it is a new listing,from the feed, then insert post as usual. Is it clear what i am after or is my explanation bad? – re-boot Oct 22 '16 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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