using post id as question slug issue

Solved7.13K viewsUpdates
0

Hi Rahul,

the following code would use post id as new question slug when user creates a new question.

It works well. However, it doesn’t work when the user edit an question.

when user edit an question, somehow the answer title would become the post slug, overwriting the slug id that was there before.

 

all i wanted is the use post id when user create a new question, or even edit an existing question. is there any action hook other than ap_after_new_question ?

thanks.

 

add_action( ‘ap_after_new_question’, ‘using_id_as_slug’, 10, 2 );
function using_id_as_slug($post_id, $post){
global $post_type;
remove_action(‘ap_after_new_question’, ‘using_id_as_slug’ );
wp_update_post(array(‘ID’ => $post_id, ‘post_name’ => ‘q’ . $post_id ));
add_action(‘ap_after_new_question’, ‘using_id_as_slug’ );
}

 

Edit —

the following solution works for me. of cause, you can refactor to remove redundant code.

 

add_action( 'ap_after_new_question', 'using_id_as_slug', 10, 2 );
add_action('ap_after_update_question', 'using_id_as_slugB', 10, 2);
function using_id_as_slug($post_id, $post){
  global $post_type;

    if (wp_is_post_revision($post_id))
      return false;
    remove_action('ap_after_new_question', 'using_id_as_slug' );
    wp_update_post(array('ID' => $post_id, 'post_name' => 'q' . $post_id ));
    add_action('ap_after_new_question', 'using_id_as_slug' );
}

function using_id_as_slugB($post_id, $post){
  global $post_type;

    if (wp_is_post_revision($post_id))
      return false;
    remove_action('ap_after_update_question', 'using_id_as_slugB' );
    wp_update_post(array('ID' => $post_id, 'post_name' => 'q' . $post_id ));
    add_action('ap_after_update_question', 'using_id_as_slugB' );
}

Please tell me what you are trying to achieve.

hi all i wanted is the use post id as post slug when user create a new question, or even edit an existing question. right now my code doesnt work when user edit question.

this is a follow up of the question i asked before https://anspress.io/questions/question/how-to-change-questionpost-slug-programatically/ it works for new question but when editing question the slug got reverted back.

hi rahul, do you have a solution? i really need it to work before i roll out my site. thanks

0

add this hook as well:

add_action('ap_after_update_question', 'using_id_as_slug’, 10, 2);
You are viewing 1 out of 3 answers, click here to view all answers.