Filter uncanny-automator

automator_wp_post_updates_post_updated

Filters the post update data after a WordPress post has been updated.

add_filter( 'automator_wp_post_updates_post_updated', $callback, 10, 3 );

Description

Fires after a WordPress post is updated. Developers can use this filter to modify the return value, preventing trigger execution if needed. It receives the post ID, the post object after the update, and the post object before the update. Return `false` to stop the trigger from running.


Usage

add_filter( 'automator_wp_post_updates_post_updated', 'your_function_name', 10, 3 );

Parameters

$post_id (mixed)
This parameter appears to be a placeholder or unused in the current context, as the actual arguments are unpacked from `$args[0]`.
$wp_post_after (mixed)
This parameter contains the ID of the post that was updated.
$wp_post_before (mixed)
This parameter contains the WordPress post object representing the state of the post *before* it was updated.

Return Value

The filtered value.


Examples

/**
 * Example callback for the automator_wp_post_updates_post_updated filter.
 * This example checks if a specific post type was updated and adds a meta field
 * if the post's title has changed.
 *
 * @param bool     $should_trigger Whether the trigger should proceed.
 * @param int      $post_id        The ID of the updated post.
 * @param WP_Post  $wp_post_after  The WP_Post object after the update.
 * @param WP_Post  $wp_post_before The WP_Post object before the update.
 * @return bool                   Whether the trigger should proceed.
 */
add_filter( 'automator_wp_post_updates_post_updated', function ( $should_trigger, $post_id, $wp_post_after, $wp_post_before ) {

	// Check if the post is of a specific type, e.g., 'post' or 'page'.
	if ( 'post' !== $wp_post_after->post_type ) {
		return $should_trigger;
	}

	// Check if the post title has actually changed.
	if ( $wp_post_after->post_title !== $wp_post_before->post_title ) {
		// You could potentially add some logic here, like marking the post for review
		// or adding a specific meta field to indicate the title change.
		// For this example, we'll just demonstrate accessing the data.
		error_log( "Post ID {$post_id} title updated from '{$wp_post_before->post_title}' to '{$wp_post_after->post_title}'." );

		// Example of adding meta data if you had a specific reason to do so.
		// update_post_meta( $post_id, '_automator_title_changed', current_time( 'mysql' ) );

		// If you want to conditionally prevent further automation based on this change,
		// you could return false here. For this example, we'll allow it to continue.
		return true;
	}

	// If the title didn't change, or if other conditions weren't met,
	// return the original value to allow the trigger to proceed as intended.
	return $should_trigger;

}, 10, 4 ); // Priority 10, accepts 4 arguments.

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/wp/triggers/wp-user-updates-post.php:121
src/integrations/wp/triggers/wp-anon-updates-post.php:126

protected function validate_trigger( ...$args ) {

		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return false;
		}

		list( $post_id, $wp_post_after, $wp_post_before ) = $args[0];

		// Prevent if publishing a post.
		if ( 'publish' === $wp_post_after->post_status && 'publish' !== $wp_post_before->post_status ) {
			return false;
		}

		$ignore_statuses = apply_filters(
			'automator_wp_post_updates_ignore_statuses',
			array(
				'trash',
				'draft',
				'future',
			),
			$post_id,
			$wp_post_after,
			$wp_post_before
		);

		// Prevent if the status is excluded
		if ( in_array( $wp_post_after->post_status, $ignore_statuses, true ) ) {
			return false;
		}

		$include_non_public_posts = apply_filters( 'automator_wp_post_updates_include_non_public_posts', false, $post_id );
		if ( false === $include_non_public_posts ) {
			$__object = get_post_type_object( $wp_post_after->post_type );
			if ( false === $__object->public ) {
				return false;
			}
		}

		if ( empty( $post_id ) ) {
			return false;
		}

		return apply_filters( 'automator_wp_post_updates_post_updated', true, $post_id, $wp_post_after, $wp_post_before );
	}


Scroll to Top