Created
November 26, 2020 22:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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