Skip to content

Instantly share code, notes, and snippets.

@evgrezanov
Last active August 2, 2021 09:41
Show Gist options
  • Save evgrezanov/44c03e61ea5a68b5c6ed507f12f57c02 to your computer and use it in GitHub Desktop.
Save evgrezanov/44c03e61ea5a68b5c6ed507f12f57c02 to your computer and use it in GitHub Desktop.
<?php
// https://gist.github.com/evgrezanov/44c03e61ea5a68b5c6ed507f12f57c02
add_action( 'pmxi_saved_post', 'mst_company_jobs_mapping', 10, 1);
function mst_company_jobs_mapping($post_id){
$import_id = ( isset( $_GET['id'] ) ? $_GET['id'] : ( isset( $_GET['import_id'] ) ? $_GET['import_id'] : 'new' ) );
if ( $import_id == '161' || $import_id == '145') :
global $wpdb;
// choоse table
$is_test = false;
if ($is_test):
$table_name = "wp_toolset_associations";
else:
$table_name = "soultm_toolset_associations";
endif;
$company_name = get_post_meta($post_id, 'wpcf-company-name-temp', true);
if ( !empty($company_name) ):
$page = get_page_by_title($company_name, OBJECT, 'company');
if (!empty($page)):
// если компания существует, свяжем
$company_id = $page->ID;
toolset_connect_posts('job-company', $company_id, $post_id);
$result = $wpdb->insert(
$table_name,
array(
'relationship_id' => 11,
'parent_id' => $company_id,
'child_id' => $post_id,
'intermediary_id' => 0
),
array('%d','%d','%d','%d')
);
else:
// новая компания - создадим черновик
// date
$date = strtotime('now');
$company_data = array(
'post_title' => wp_strip_all_tags( $company_name ),
'post_content' => 'new company',
'post_status' => 'draft',
'post_type' => 'company',
'post_date' => strftime("%Y-%m-%d %H:%M:%S", $date),
);
$new_company_id = wp_insert_post( $company_data );
if( is_wp_error($new_company_id) ){
error_log(print_r($new_company_id->get_error_message(), false));
} else {
toolset_connect_posts( 'job-company', $new_company_id, $post_id);
$result = $wpdb->insert(
$table_name,
array(
'relationship_id' => 11,
'parent_id' => $new_company_id,
'child_id' => $post_id,
'intermediary_id' => 0
),
array('%d','%d','%d','%d')
);
}
endif;
// rewrite job slug
$job = get_post($post_id);
$job_slug = sanitize_title($job->post_name, $post_id);
$company_name = strtolower(stripslashes($company_name));
$new_slug = $company_name . '-' . $job_slug;
$new_slug = wp_unique_post_slug($new_slug, $post_id, $job->post_status, 'job', 0);
if ($new_slug !== $job->post_name){
wp_update_post(array(
'ID' => $post_id,
'post_name' => $new_slug
));
}
endif;
endif;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment