using post id as question slug issue

Solved7.08K 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);
0

Now this new code is working. Thank you so much.

But another problem is, this makes “AnsPress Email” sending mail 2 more time :(.

The email topic was,

  1. New question posted by [USER]
  2. A question is edited by [USER]
  3. A question is edit by [USER]

Do you have any suggestion for other hooks?

Thank you so much.

hmm, that i dont know. @Rahul, could you please let up know the solution? thanks

Use this filters: ap_processed_new_question and ap_processed_update_question instead of ap_after_new_question and ap_after_update_question

@Rahul

Thank you so much but it still sent 3 emails. Then I changed priority from 10 to 9 and it made infinite loop. Then I reverted to default slug instead. >_<

0

Dear Rahul and Kenny,

I use this code and my server was down with infinite loop (it kept sending notification mail to me because I’ve installed “AnsPress Email”)

add_action('ap_after_new_question', 'using_id_as_slug', 10, 2 );
add_action('ap_after_update_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' );
}

Do you have any suggestion? Thank you so much.

I had the same issue before. i have updated my question and put working code there. for some reason, you cant call the same function in after new question hook and after update question hook.

@kenny – Oh, thank you so much 🙂