Is it possible to auto-populate the post title?
I'm using WooCommerce (so it's a Custom Post Type) and I'd like to the title to be
Post tag + product_cat + product_tag
I've had some feedback that it's "not a recommended method". However I'm simply trying to do 2 things ....
1) <strong>Reduce duplicate input</strong>: The titles for this website are the exact combination of the Post tag + (CPT) product_cat & product_tag. So --while me complaining that typing a title is duplicate workload-- it is duplicate workload! And it seems more absurd that you have to type the title in when all the info has been registered through the terms already.
2) <strong>Force uniformity in titles</strong>. Certain shopping carts need uniform titles for their catalogue of products, and this cart is one of them. In this instance the website sells wine, and there are 3 elements to each wine title (year + winery + blend). So that's how I've structured the Tag, product_cat and product_tag terms: post tag = year (of wine), product_cat = winery, product_tag = blend (of wine). Therefore the product will only ever have a maximum of 1 tag, 1 product_cat & (possibly) a few product_tags.
Joshua Nelson answers:
Do you require the post title to have this, or could you edit where the product is displayed to display the "year + windery + blend" data instead of the post title? This might be easier than trying to auto-populate the post title (and then your post title could be the ISBN or product number, etc).
Check these out:
[[LINK href="http://wordpress.org/extend/plugins/auto-post-title/"]]http://wordpress.org/extend/plugins/auto-post-title/[[/LINK]]
[[LINK href="http://wordpress.stackexchange.com/questions/26616/automatically-generate-custom-post-title-based-on-meta"]]http://wordpress.stackexchange.com/questions/26616/automatically-generate-custom-post-title-based-on-meta[[/LINK]]
TheLoneCuber comments:
Yes I have managed to display the tag+product_cat+product_tag for on-page (both single-product.php titles, and for loop-shop.php titles). But that doesn't help with the backend of WordPress - whereby a blank-titled CPT causes much grief.
Re: the plugin - it does modify all the post titles perfectly. But it only writes the title the title for the backend post view (/wp-admin/edit.php?post_type=product) and does not write anything to the post title in the single view (/wp-admin/post.php?post=64&action=edit).
Re: [[LINK href="http://wordpress.stackexchange.com/questions/26616/automatically-generate-custom-post-title-based-on-meta"]]WPSE[[/LINK]] link. That sounds like what I need, but is beyond my coding skills. Can you convert it for my use?
TheLoneCuber comments:
It seems there is an [[LINK href="http://wordpress.stackexchange.com/questions/6406/dynamically-set-wordpress-post-title-to-the-category-name"]]answer on WPSE[[/LINK]]. Can anyone mod this to use Post tag + product_cat + product_tag?
Jurre Hanema answers:
Wouldn't it be easier to use a WooCommerce attribute instead of the post_tag for the year?
Anyway, try this:
<?php
add_action('save_post', 'wpq_woo_set_title');
function wpq_woo_set_title($post_id)
{
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if(wp_is_post_revision($post_id) || get_post_type($post_id) != 'product')
return;
$post_title = '';
$title_parts = array(
get_the_terms($post_id, 'post_tag'),
get_the_terms($post_id, 'product_cat'),
get_the_terms($post_id, 'product_tag')
);
foreach($title_parts as $i => $tp)
{
$title_parts[$i] = implode(', ', wp_list_pluck($tp, 'name'));
}
$title_parts = array_filter($title_parts, create_function('$x', 'return !empty($x);'));
$post_title = implode(' - ', $title_parts);
if(!$post_title)
$post_title = 'Product #'.$post_id;
remove_action('save_post', 'wpq_woo_set_title');
wp_update_post(
array(
'ID' => $post_id,
'post_title' => $post_title
)
);
add_action('save_post', 'wpq_woo_set_title');
}
?>