Hi there and thanks for your message. I’ve took some time and built two custom functions for you that should do exactly what you had in mind.
The first function will loop through all post types (you can define which post types are being used), checks whether it contains a secondary title and if not, it’ll remove the meta field from the database. This is useful if you want to process a lot of posts at the same time. Here’s the link to the function:
<?php
/**
* This function loops through ALL post types defined in the
* $post_types array and checks whether each post hsa a secondary post title.
* If not, it will remove the meta field; if yes, nothing will happen.
*
* @param array $post_types
*/
function secondary_title_remove_meta_field($post_types = array()) {
/** If no post types are set, use "post" and "page" as default */
if(!$post_types) {
$post_types = array(
"post",
"page"
);
}
/** Fetch posts that contain a secondary title meta field */
$posts = get_posts(
array(
"post_type" => $post_types,
"showposts" => -1,
"meta_key" => "_secondary_title"
)
);
/** Loop through them and remove the meta field if the secondary title is empty */
foreach((array)$posts as $post) {
$secondary_title = get_post_meta($post->ID, "_secondary_title", true);
if(!$secondary_title) {
delete_post_meta($post->ID, "_secondary_title");
}
}
}
/** This action hook will activate the function */
add_action("admin_head", "secondary_title_remove_meta_field");
Example usage:
<?php
/**
* Remove empty secondary title meta field for WordPress' natove
* "post" post type and for "movie" and "book" custom post types previously registered.
*/
secondary_title_remove_meta_field(
array(
"post",
"movie",
"book"
)
);
The second one is an action hook that is being triggered whenever you save or edit a custom post type. For example, if you create a post and you enter a secondary title, it’ll be saved. If you then decide to remove the secondary title and hit “Update”, it will remove the meta field from the database. The function can be found here:
<?php
/**
* This function will be executed whenever a post is being created or updated.
* It will check whether the secondary post exists and if not, it will remove
* the corresponding meta field from the database. When a secondary title is
* being insert at a later point, it will be created again.
*
* @param $id
*/
function secondary_title_remove_meta_field_on_edit($id) {
/** Check if the saved/edited post has a secondary title meta field */
$secondary_title = get_post_meta($id, "_secondary_title", true);
/** If yes and the secondary title is empty, remove the meta field from the database */
if(!$secondary_title) {
delete_post_meta($id, "_secondary_title");
}
}
Example usage:
<?php
/** Do it when a post is saved */
add_action("save_post", "secondary_title_remove_meta_field_on_edit");
/** Do it when a post is edited */
add_action("edit_post", "secondary_title_remove_meta_field_on_edit");
You can put the code you need into the functions.php oof your theme. If you encounter any errors, please let me know. If everything works fine and you’re happy with my support, I’d be more than glad if you could leave a quick review ??