What hook should I use if I want to trigger an event after post is published?

Solved7.38K viewsWordPress
0

I’m working on an extension and would like to confirm the correct hook for triggering an event after a post has been published? Curious about both question and answer custom post types.

1
	public function after_new_question($post_id, $post) {
	    update_post_meta( $post_id, ANSPRESS_VOTE_META, '0' );
	    update_post_meta( $post_id, ANSPRESS_SUBSCRIBER_META, '0' );
	    update_post_meta( $post_id, ANSPRESS_CLOSE_META, '0' );
	    update_post_meta( $post_id, ANSPRESS_FLAG_META, '0' );
	    update_post_meta( $post_id, ANSPRESS_VIEW_META, '0' );
	    update_post_meta( $post_id, ANSPRESS_UPDATED_META, current_time( 'mysql' ) );
	    update_post_meta( $post_id, ANSPRESS_SELECTED_META, false );

		// Update answer count.
		update_post_meta( $post_id, ANSPRESS_ANS_META, '0' );

		// Update user question count meta.
	    ap_update_user_questions_count_meta( $post_id );

		/**
		 * ACTION: ap_after_new_question
		 * action triggered after inserting a question
		 * @since 0.9
		 */
		do_action( 'ap_after_new_question', $post_id, $post );
	}
	public function after_new_answer($post_id, $post) {
	    $question = get_post( $post->post_parent );

		// Set default value for meta.
		update_post_meta( $post_id, ANSPRESS_VOTE_META, '0' );

		// Set updated meta for sorting purpose.
		update_post_meta( $question->ID, ANSPRESS_UPDATED_META, current_time( 'mysql' ) );
	    update_post_meta( $post_id, ANSPRESS_UPDATED_META, current_time( 'mysql' ) );

		// Get existing answer count.
		$current_ans = ap_count_published_answers( $question->ID );

		// Update answer count.
		update_post_meta( $question->ID, ANSPRESS_ANS_META, $current_ans );
	    update_post_meta( $post_id, ANSPRESS_BEST_META, 0 );
	    ap_update_user_answers_count_meta( $post_id );

		/**
		 * ACTION: ap_after_new_answer
		 * action triggered after inserting an answer
		 * @since 0.9
		 */
		do_action( 'ap_after_new_answer', $post_id, $post );
	}

Assuming developers are not insane, ap_after_new_question and ap_after_new_answer should do exactly what you want.

It’s all in sources.

You could also use ap_processed_new_question and ap_processed_new_answer if you want your code to be executed after question or answer was added, but meta was not yet processed.
I don’t see any good reason to do it, though.

Thank you! This is exactly what I was looking for.

0

Hello,

Is there any guide on how to extends these please? I need to make a curl request after new answer.

commented on answer

ok nvm I worked directly in hooks.php. There’s probably a better way but for now it works.

1

It goes like this:

process_form()
process_ask_form()
    wp_insert_post()
add_action( ‘save_post’, ‘action_on_new_post’)
action_on_new_post()
    do_action( ‘ap_processed_new_question’)
add_action( ‘ap_processed_new_question’, ‘after_new_question’)
after_new_question()
    do_action( ‘ap_after_new_question’) // <---
add_action( ‘ap_after_new_question’, ‘new_question’ )
new_question()
add_action( ‘ap_after_new_question’, ‘new_question’ ); //reputation
new_question() //reputation

Really simple!

Is there a software in PHP world that would build such maps/stacks for me automatically? Maybe some easy real time php debugger?

@Dima…Greetings please. How do I do this. I want to add an item before “Title” in the ask a question page. The item I want to add is (Note: bla bla bla some caveat emptors here). I hope you understand this. I know that there is a second tap (How to Ask) but I don’t want to use that. Help please!!! And don’t forget that am a newbie!!! So explain your steps or the steps I should take to make it work. Thanks.

Hi @siconet. I’m not an employee here, just answering curious questions as a part of my training in PHP/Wordpress/Anspress. I think you should try asking general public here, not just me.

If you want to add a static text to one of pages, you just override that page’s template (copy it to your theme’s /anspress/ folder), add your text in your template.

In case you want to show your text above title which is rendered in base page template, you could use CSS to make this text appear where you want it, or you may edit base template to conditionally display that text, or you could embed widgets code in there, and use a conditional widget.

Proper solution would be to not output any text above the page title. Because, it’s a title. It should go first.

@Dima…thanks. Asked you specifically because you respond, which makes things flow faster for some of us newbies. Will direct questions to general house next time. Will try your suggestions above and revert. Thanks.