Skip to content

Instantly share code, notes, and snippets.

@davidperezgar
Created July 6, 2021 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidperezgar/40e5697cb9c389d0d68cbf86275136e8 to your computer and use it in GitHub Desktop.
Save davidperezgar/40e5697cb9c389d0d68cbf86275136e8 to your computer and use it in GitHub Desktop.
Updates meta and adds text to taxonomy
<?php
define('WP_USE_THEMES', false);
require_once('wp-load.php');
echo 'iniciado proceso.<br />';
/**
* Finds product categories ids from array of names given
*
* @param array $taxonomy_names Array of names.
* @return string IDS of categories.
*/
function find_categories_ids( $taxonomy_names, $taxonomy ) {
$level = 0;
$cats_ids = array();
foreach ( $taxonomy_names as $taxonomy_name ) {
$cat_slug = sanitize_title( $taxonomy_name );
$taxonomy_obj = get_term_by( 'slug', $cat_slug, $taxonomy );
if ( $taxonomy_obj ) {
// Finds the category.
$cats_ids[ $level ] = $taxonomy_obj->term_id;
} else {
$parent_prod_id = 0;
if ( $level > 0 ) {
$parent_prod_id = $cats_ids[ $level-1 ];
}
// Creates the category.
$term = wp_insert_term(
$taxonomy_name,
$taxonomy,
array(
'slug' => $cat_slug,
'parent' => $parent_prod_id,
)
);
if ( ! is_wp_error( $term ) ) {
$cats_ids[ $level ] = $term['term_id'];
}
}
$level++;
}
return $cats_ids;
}
$examposts = get_posts('posts_per_page=-1&post_type=ttshowcase&fields=ids');
if ( !empty( $examposts ) ) {
foreach($examposts as $post_id){
// Linkedin
$alum_linkedin = get_post_meta( $post_id, 'contacta_linkedin', true );
$alum_rating = get_post_meta( $post_id, 'valoracion', true );
$alum_programa = get_post_meta( $post_id, 'programa', true );
$alum_promocion = get_post_meta( $post_id, 'promocion', true );
$alum_content = get_post_meta( $post_id, '_aditional_info_short_testimonial', true );
$alum_titulacion = get_post_meta( $post_id, 'titulacion', true );
$programa_id = '';
if ( $alum_programa ) {
$args = array( 'post_type' => 'programa', 's' => $alum_programa );
$query = get_posts( $args );
if ( count( $query) > 0 ) {
$programa_id = $query[0]->ID;
}
}
if ( $alum_promocion ) {
$roman = explode( ' ', $alum_promocion );
$roman_values = array(
'I' => 1,
'II' => 2,
'III' => 3,
'IV' => 4,
'V' => 5,
'VI' => 6,
'VII' => 7,
'VIII' => 8,
'IX' => 9,
'X' => 10,
'XI' => 11,
'XII' => 12,
'XIII' => 13,
'XIV' => 14,
'XV' => 15,
'L' => 50,
'C' => 100,
'D' => 500,
'M' => 1000,
);
$promocion_num = $roman_values[ $roman[0] ];
} else {
$promocion_num = '';
}
update_post_meta( $post_id, 'alum_linkedin', $alum_linkedin );
update_post_meta( $post_id, 'alum_rating', $alum_rating );
update_post_meta( $post_id, 'alum_programa', $programa_id );
update_post_meta( $post_id, 'alum_promocion', $promocion_num );
$my_post = array(
'ID' => $post_id,
'post_content' => $alum_content,
);
// Update the post into the database.
wp_update_post( $my_post );
// Category.
$cats_ids_term = find_categories_ids( array( $alum_titulacion ), 'titulacion' );
wp_set_post_terms(
$post_id,
$cats_ids_term,
'titulacion'
);
echo '---<br />';
echo 'PostID: ' . $post_id . '<br/>';
echo 'Alumno: ' . get_the_title( $post_id ) . '</br>';
echo 'Programa ID: ' . $programa_id . '</br>';
echo 'Promoción: ' . $promocion_num . '</br>';
echo 'Titulación anterior: ' . $alum_titulacion . '</br>';
echo 'Titulación ID: ' . implode( ',', $cats_ids_term ) . '</br>';
// Delete post meta.
delete_post_meta( $post_id, '_aditional_info_custom_boolean' );
delete_post_meta( $post_id, '_aditional_info_custom_boolean_2' );
delete_post_meta( $post_id, '_aditional_info_custom_boolean_3' );
delete_post_meta( $post_id, '_aditional_info_custom_boolean_4' );
delete_post_meta( $post_id, '_aditional_info_email' );
delete_post_meta( $post_id, '_aditional_info_name' );
delete_post_meta( $post_id, '_aditional_info_rating' );
delete_post_meta( $post_id, '_aditional_info_review_title' );
delete_post_meta( $post_id, '_aditional_info_short_testimonial' );
delete_post_meta( $post_id, '_aditional_info_target' );
delete_post_meta( $post_id, '_aditional_info_url' );
delete_post_meta( $post_id, '_advanced_info_custom_url' );
delete_post_meta( $post_id, '_advanced_info_target' );
delete_post_meta( $post_id, '_answer_info_answer' );
delete_post_meta( $post_id, '_answer_info_notes' );
delete_post_meta( $post_id, '_cargo-alumno' );
delete_post_meta( $post_id, '_contacta_linkedin' );
delete_post_meta( $post_id, '_edit_last' );
delete_post_meta( $post_id, '_edit_lock' );
delete_post_meta( $post_id, '_inscrito_registro' );
delete_post_meta( $post_id, '_programa' );
delete_post_meta( $post_id, '_promocion' );
delete_post_meta( $post_id, '_thumbnail_id' );
delete_post_meta( $post_id, '_titulacion' );
delete_post_meta( $post_id, '_valoracion' );
delete_post_meta( $post_id, '_wp_old_date' );
delete_post_meta( $post_id, '_wp_page_template' );
delete_post_meta( $post_id, '_yoast_wpseo_content_score' );
delete_post_meta( $post_id, '_yoast_wpseo_estimated-reading-time-minutes' );
delete_post_meta( $post_id, '_yoast_wpseo_meta-robots-noindex' );
delete_post_meta( $post_id, '_yoast_wpseo_primary_category' );
delete_post_meta( $post_id, 'alum_rating' );
delete_post_meta( $post_id, 'cargo-alumno' );
delete_post_meta( $post_id, 'contacta_linkedin' );
delete_post_meta( $post_id, 'inline_featured_image' );
delete_post_meta( $post_id, 'inscrito_registro' );
delete_post_meta( $post_id, 'programa' );
delete_post_meta( $post_id, 'promocion' );
delete_post_meta( $post_id, 'rank_math_advanced_robots' );
delete_post_meta( $post_id, 'rank_math_analytic_object_id' );
delete_post_meta( $post_id, 'rank_math_facebook_enable_image_overlay' );
delete_post_meta( $post_id, 'rank_math_facebook_image_overlay' );
delete_post_meta( $post_id, 'rank_math_internal_links_processed' );
delete_post_meta( $post_id, 'rank_math_permalink' );
delete_post_meta( $post_id, 'rank_math_primary_category' );
delete_post_meta( $post_id, 'rank_math_primary_titulacion' );
delete_post_meta( $post_id, 'rank_math_primary_ttshowcase_groups' );
delete_post_meta( $post_id, 'rank_math_robots' );
delete_post_meta( $post_id, 'rank_math_seo_score' );
delete_post_meta( $post_id, 'rank_math_title' );
delete_post_meta( $post_id, 'rank_math_twitter_card_type' );
delete_post_meta( $post_id, 'rank_math_twitter_enable_image_overlay' );
delete_post_meta( $post_id, 'rank_math_twitter_image_overlay' );
delete_post_meta( $post_id, 'rank_math_twitter_use_facebook' );
delete_post_meta( $post_id, 'titulacion' );
delete_post_meta( $post_id, 'valoracion' );
set_post_type( $post_id, 'alumno' );
}
}
echo 'finalizado proceso.<br />';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment