Skip to content

Instantly share code, notes, and snippets.

@mishterk
Created November 26, 2020 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishterk/3e3e526a7a5d60c029fd51af41592ce6 to your computer and use it in GitHub Desktop.
Save mishterk/3e3e526a7a5d60c029fd51af41592ce6 to your computer and use it in GitHub Desktop.
Prefixing a WordPress post title with the ID of an ACF Custom Database Tables meta data row.
<?php
add_action( 'acf/save_post', function ( $post_id ) {
global $wpdb;
// Get the meta ID from the custom DB table.
$table_name = 'my_custom_db_table';
$meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT `id` FROM {$wpdb->prefix}{$table_name} WHERE `post_id` = %d", $post_id ) );
// Choose a prefix format.
$title_prefix = "$meta_id";
$post = get_post( $post_id );
// If post title is already prefixed with meta ID, don't do it again.
if ( 0 === strpos( $post->post_title, $title_prefix ) ) {
return;
}
// Update the post.
$post->post_title = $title_prefix . $post->post_title;
wp_update_post( $post );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment