I try to assign custom taxonomies to a page when newly added by the "publish" button.
This is the function:
function set_default_object_terms( $id, $post ) {
if ( 'publish' === $post->post_status ) {
log_me ('From inside function: '.__FUNCTION__.', while I pressed the "publish" button. Post-ID: '.$id);
$taxonomy_ar = get_terms( 'property-features', '' );
foreach ($taxonomy_ar as $taxonomy_term) {
log_me ('Inside the function: '.__FUNCTION__.' and inside the "foreach"-loop for the ID: '.$id.' and Term: '. $taxonomy_term->name . ' and Post-ID :'. $post->ID);
wp_set_object_terms( $post->ID, $taxonomy_ar, $taxonomy_term->name, true );
}
}}
and here is the hook:
add_action( 'save_post', 'set_default_object_terms', 100, 2 );
In the logfile which i added to figure out if i find all my values, all my custom taxonomies get found:
[13-May-12 16:28] be in function while "publish" is pressed with this id: **64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Kitchen** and Post-ID :**64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Stove** and Post-ID :**64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Pets ok** and Post-ID :**64**
But it does not assign it. Does somebody know where the trick is?
I try to assign custom taxonomies to a page when newly added by the "publish" button.
This is the function:
function set_default_object_terms( $id, $post ) {
if ( 'publish' === $post->post_status ) {
log_me ('From inside function: '.__FUNCTION__.', while I pressed the "publish" button. Post-ID: '.$id);
$taxonomy_ar = get_terms( 'property-features', '' );
foreach ($taxonomy_ar as $taxonomy_term) {
log_me ('Inside the function: '.__FUNCTION__.' and inside the "foreach"-loop for the ID: '.$id.' and Term: '. $taxonomy_term->name . ' and Post-ID :'. $post->ID);
wp_set_object_terms( $post->ID, $taxonomy_ar, $taxonomy_term->name, true );
}
}}
and here is the hook:
add_action( 'save_post', 'set_default_object_terms', 100, 2 );
In the logfile which i added to figure out if i find all my values, all my custom taxonomies get found:
[13-May-12 16:28] be in function while "publish" is pressed with this id: **64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Kitchen** and Post-ID :**64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Stove** and Post-ID :**64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Pets ok** and Post-ID :**64**
But it does not assign it. Does somebody know where the trick is?
Share Improve this question edited May 13, 2012 at 13:11 kaiser 50.9k27 gold badges151 silver badges245 bronze badges asked May 13, 2012 at 9:48 JSSJSS 854 silver badges10 bronze badges 4 |2 Answers
Reset to default 2You are using wp_set_object_terms
wrong, the second parameter should be the term slug or id and the 3 parameter should be the taxonomy name, so try:
function set_default_object_terms( $id, $post ) {
if ( 'publish' === $post->post_status ) {
log_me ('be in function while "publish" i pressed with this id: '.$id);
$taxonomy_ar = get_terms( 'property-features' );
if (count($taxonomy_ar) > 0){
foreach ($taxonomy_ar as $taxonomy_term) {
//create an arry with all term ids
log_me ('In the "foreach" with id: '.$id.' and Term: '. $taxonomy_term->name . ' and Post-ID :'. $post->ID);
$term_ids[] = $taxonomy_term->ID;
}
wp_set_object_terms($post->ID,$term_ids,'property-features',true);
}
}
}
and make sure you register the taxonomy for pages type using register_taxonomy_for_object_type
ex:
register_taxonomy_for_object_type('property-features','page');
I've got a snippet that does this on load of the edit page, instead of on save. (So the user can see the selection before they hit publish, no surprise default terms being assigned on post_save).
I had a function changing the checkboxes to radio buttons already but it seemed like the appropriate place to also add checked
to one of the inputs.
Works on Add new, works on edit of an existing item.
// Press type taxonomy
// - Replace checkboxes for radio buttons
// - Select "Press release" by default
add_filter('wp_terms_checklist_args', 'press_type_radio_checklist');
function press_type_radio_checklist($args)
{
$radio_taxonomies = [
'press_type',
];
if (!empty($args['taxonomy']) && in_array($args['taxonomy'], $radio_taxonomies)) {
if (empty($args['walker']) || is_a($args['walker'], 'Walker')) {
// Radio button taxonomy walker
if (!class_exists('Walker_Radio_Checklist')) {
class Walker_Radio_Checklist extends Walker_Category_Checklist
{
function walk($elements, $max_depth, $args = [])
{
$output = parent::walk($elements, $max_depth, $args);
$output = str_replace([
'type="checkbox"',
"type='checkbox'",
], [
'type="radio"',
"type='radio'",
], $output);
// If there aren't any selected cats, select the default
if (empty($args['selected_cats'])) {
$default_term = get_term_by('name', 'Press release', 'press_type');
if (!empty($default_term) && $default_term instanceof WP_Term) {
$default_term_id = $default_term->term_id;
$output = str_replace([
'input value="' . $default_term_id . '"',
"input value='$default_term_id'",
], [
'input value="' . $default_term_id . '" checked',
"input value='$default_term_id' checked",
], $output);
}
}
return $output;
}
}
}
$args['walker'] = new Walker_Radio_Checklist;
}
}
return $args;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745303077a4621555.html
log_me()
? – kaiser Commented May 13, 2012 at 13:09define( 'WP_DEBUG_LOG', true );
. – kaiser Commented May 13, 2012 at 15:46