cosmoloco
Tengo un requisito en WordPress, donde, si la cadena de términos de taxonomía es igual al título de la publicación, actualice el slug (ya que las reglas de enlace permanente fallan en este caso).
mi código es:
add_action('save_post', 'change_default_slug', 10,2);
function change_default_slug($post_id, $post) {
error_log($post_id);
error_log($post->post_title);
error_log($post->post_name);
$taxonomies=get_taxonomies('','names');
$terms = wp_get_post_terms($post->ID, $taxonomies, array("fields" => "names"));
$terms = array_map('strtolower', $terms);
error_log('terms :' . json_encode($terms));
$title = strtolower($post->post_title);
error_log('title : ' . $title);
if(in_array($title, $terms)) {
error_log('yessss');
$args = array (
'ID' => $post->ID,
'post_name' => $post->post_name . "-post"
);
$result = wp_update_post($update_args);
error_log('result');
error_log(json_encode($result));
} else {
error_log('noooooooo');
}
}
En la publicación requerida, obtengo registros: Yesss result 0. El slug no se actualiza. Por favor ayuda en lo mismo. He probado literalmente todas las soluciones disponibles para este problema. Tiene que hacerse a través de functions.php
Finalmente pude resolverlo usando: wp_insertar_post()
$taxonomies=get_taxonomies('','names');
$terms = wp_get_post_terms($post->ID, $taxonomies, array("fields" => "all"));
foreach($terms as $term) {
if($term->taxonomy == 'category'){
$categories[] = $term->term_id;
} else if($term->taxonomy == 'post_tag'){
$tags[] = $term->term_id;
}
}
.
.
.
//detach the hook to avoid infinite looping of the hook on post insert
remove_action('save_post', 'change_default_slug', 10,2);
//insert post
$result = wp_insert_post($post, true);
//attach post tags to the current post (since not by default attached)
wp_set_post_terms($post_id,$tags,'post_tag');
//attach post categories to the current post (since not by default attached)
wp_set_post_terms($post_id,$categories,'category');
//re-activate the hook
add_action('save_post', 'change_default_slug', 10,2);
-
Debe proporcionar los parámetros en variable
$post
para que esto tenga sentido para los lectores.– Jomar Sevillajo
18 de abril de 2020 a las 7:29
¿Ha sido útil esta solución?
Tu feedback nos ayuda a saber si la solución es correcta y está funcionando. De esta manera podemos revisar y corregir el contenido.
$resultado = wp_update_post($update_args); debe ser $resultado = wp_update_post(args);
– Jürgen Schulze
20 de mayo de 2017 a las 7:55