Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Wondering if anyone can spot the obvious mistake or give me some guidance on where I'm going wrong.

I should also state this is my first plugin with Wordpress, so I'm sure I have overlooked something.

Effectively what I am trying to do is get data from a non WPDB table (but within the same DB), take that data and add in into WordPress custom post type, and repeat this process every hour, number of rows could be anywhere from 220-350.

The custom post type is created and all is working fine, this script also needs to run every hour which is what I was working on currently. Scheduling a cron job is not the issue and the below code executes fine when there is 45-50 or less, once you get over that the script fails. (I have also tried: ini_set('max_execution_time', 300) )

If you need any me to provide any further info, let me know.

The below is my code I am working with.

function insert_posts()

 * Check for the latest jobs from tbljobs
 * Need to firstly delete all the current jobs in the wpdb posts table
 * Create the new posts
 *  - once added, add in all the meta data  

global $wpdb;
$q = "SELECT * FROM tbljobs WHERE job_active = 1";
$results = $wpdb->get_results($q, OBJECT);

// print_r($results);die();

if (!empty($results) )
    // delete the current set of information
    $args = array('posts_per_page'=>-1, 'post_type' => 'sw_jobs');
    $current_jobs = get_posts( $args );

    foreach ( $current_jobs as $post )
        wp_delete_post($post->ID, true);

    foreach ( $results as $r) 
        $job_post = array(
            'post_title'    => $r->job_title,
            'post_name'     => $r->job_id . "-" . sanitize_title_with_dashes($r->job_title),
            'post_type'     => 'sw_jobs',
            'post_content'  => $r->job_description,
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_excerpt'  => $r->job_summary,

        $new_post_id = wp_insert_post( $job_post );

        wp_set_object_terms( $new_post_id, validate_term($r->job_sub_category, 'classification'), 'classification', true );
        wp_set_object_terms( $new_post_id, validate_term($r->job_location, 'location'), 'location', true );
        wp_set_object_terms( $new_post_id, validate_term($r->job_work_type, 'worktype'), 'worktype', true );

        // now we need to check the job tags
        if ( !empty($r->job_tags) )
            // ok we have at last one tag so leasts do something
            $job_tags = explode( ',', $r->job_tags );
            foreach ( $job_tags as $t )
                wp_set_object_terms( $new_post_id, validate_term($t, 'keyword'), 'keyword', true );


        // now that the post is in and categories all set, lets update all the meta content
        $sw_job_details = array(
                'job_hotjob' => $r->job_hotjob,
                'job_new' => $r->job_new,
                'job_id' => $r->job_id,
                'job_reference' => $r->job_reference,
                'job_post_date' => $r->job_post_date,
                'job_owner' => $r->job_owner,
                'job_application_email' => $r->job_application_email,
                'job_client' => $r->job_client,
                'job_salary_type' => $r->job_salary_type,
                'job_salary_min' => $r->job_salary_min,
                'job_salary_max' => $r->job_salary_max,
                'job_govt_role' => $r->job_govt_role,
                'job_apply_url' => $r->job_apply_url
        update_post_meta($new_post_id, 'sw_job_details', $sw_job_details);
    $update = true;
    $update = false;

// keep during testing, just checking to see if the amount of jobs that were selected form ext db
if ($update)
    wp_mail('my-email@gmail.com', 'Upload-'.time(), "Uploaded all the data at ".time() . "with a count of ".$count );
    wp_mail('my-email@gmail.com', 'Failed', "Uploaded failed at ".time() );


// term_name = the term name we are checking
// taxonomy_name = the taxonomy it belongs to
// return = do we want the id returned or the term name, default is name. options are     ('name', 'id')
function validate_term($term_name, $taxonomy_name, $return = 'name')
// idea of this function is to take the term name above check to see if it exists in the taxonomy, 
// if not create it, and return the term id either way
$term = term_exists($term_name, $taxonomy_name);
if ($term !== 0 && $term !== null) 
    $tid = $term['term_id'];
    // no term exists, so create one
    $term = wp_insert_term( $term_name, $taxonomy_name, array('slug' => sanitize_title_with_dashes(preg_replace('/\//', '-', $term_name))) );
    $tid = $term['term_id'];

return $return === 'id' ? $tid : $term_name;
share|improve this question
add comment

Your Answer


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

Browse other questions tagged or ask your own question.