admin管理员组文章数量:1419198
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;
}
本文标签: save postHow to assign default taxonomy to pages on 39savepost39
版权声明:本文标题:save post - How to assign default taxonomy to pages on 'save_post'? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745303076a2652489.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
log_me()
? – kaiser Commented May 13, 2012 at 13:09define( 'WP_DEBUG_LOG', true );
. – kaiser Commented May 13, 2012 at 15:46