Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

assign multiple taxonomy terms according to certain categories. WordPress

  • REFUNDED

Hi, I need to assign multiple taxonomy terms according to certain categories.
Here is the outline of my "function" and my question is:
- How to make my conditional tags?
That's it! Hope someone can provide quick help.


add_action( 'save_post', 'ifk_set_default_object_terms', 100, 2 );

function ifk_set_default_object_terms() {
global $post;

if ( !current_user_can( 'edit_post', $post->ID ))// - check permissions
return $post->ID;

if( 'test' === $post->post_type ) {// - check post_type

if( 'publish' === $post->post_status ) {// - Just publish

$cats = wp_set_post_categories( $post);

if( ?????) { // call for a term slug or id
wp_set_object_terms( $post_id, 'foo', 'post_tag' );
wp_set_object_terms( $post_id, 'bar', 'post_tag' );
}

if( ?????) { // call for a term slug or id
wp_set_object_terms( $post_id, 'foo', 'post_tag' );
wp_set_object_terms( $post_id, 'bar', 'post_tag' );
}
}
}
}

Answers (5)

2011-09-14

Fahad Murtaza answers:

Can you explain a bit more.


zefranck comments:

such a position that is in the category "bar" and awaiting relecure Must also be connected to the category "foo" on the hook save_post

Sorry for my English


Fahad Murtaza comments:

I see, there is a little issue with how you are calling

$cats = wp_set_post_categories( $post);

which is not in the right syntax. Please check the documentation for the function wp_set_post_categories i.e [[LINK href="http://codex.wordpress.org/Function_Reference/wp_set_post_categories"]]http://codex.wordpress.org/Function_Reference/wp_set_post_categories[[/LINK]]

As you can see, you have to do something like

wp_set_post_categories( $post_ID, $post_categories )

where for you, this code will be something like

<?php
//.....
// List of categories id numbers.
$post_categories=array(1, 2,3);
// This string is based on your single or multiple categories, I think single category ID in your case

wp_set_post_categories( $post->ID, $post_categories );

//or if you are only going to use the default category i.e with ID = 0, then
$cats = wp_set_post_categories( $post->ID);

// No second parameter above which defaults to 0
// Now we already have the categories which we stored as the post category(ies)

if($cats) { // If category(ies) saved properly

// $post_categories has the IDS for all categories that we saved above
// Now based on that, we can code something like this
// Since we have category ID(s) as passed in array above, we better use category ID(s) below in condition
// in_category which check if the current post is in a certain catgeory

if(in_category($any_category_id,$post->ID)) { // call for a term slug or id

wp_set_object_terms( $post_id, 'foo', 'post_tag' );

wp_set_object_terms( $post_id, 'bar', 'post_tag' );

}



if(in_category($any__other_category_id,$post->ID) { // call for a term slug or id

wp_set_object_terms( $post_id, 'foo', 'post_tag' );

wp_set_object_terms( $post_id, 'bar', 'post_tag' );

}

}


?>


I hope that helps. For slug based conditionals, Jurre Hanema's answer will help.


zefranck comments:

Well, this code works,
function ifk_set_default_object_terms($post_id, $post) {

if( 'test' === $post->post_type ) {// - check post_type

if( 'publish' === $post->post_status ) {// - Just publish

if(in_category('cat3', $post_id )) { // call for a term slug or id
wp_set_object_terms( $post_id, 'foo', 'post_tag' );
wp_set_object_terms( $post_id, 'bar', 'post_tag' );
}

if(in_category('cat4', $post_id )) {// call for a term slug or id
wp_set_object_terms( $post_id, 'tag2', 'post_tag' );
wp_set_object_terms( $post_id, 'bar2', 'post_tag' );
}
if(in_category( 1 , $post_id )) {
wp_set_object_terms( $post_id, NULL, 'post_tag' );
}
}
}
}

it creates a relationship only for the last<em>wp_set_object_terms</em>.
The ideal would be for me that I could put a conditional tag in each table.
$defaults = array(

'post_tag' => array( 'taco', 'banana' ),

'monkey-faces' => array( 'see-no-evil' ),

);

and used a foreach loop at the end of the function but obviously I'm not good with foreach loop.

Ps: sorry for being late

2011-09-14

Jurre Hanema answers:

To be honest I don't really understand the question, but I guess you need something like this


if(is_object_in_term($post_id, 'taxonomy_you_want_to_check', array('slugs', 'of', 'the', 'terms', 'to', 'check'))
{
// Do something
}

2011-09-14

Luis Abarca answers:

umm, maybe this way ?

add_action( 'save_post', 'ifk_set_default_object_terms', 100, 2 );

function ifk_set_default_object_terms()
{
global $post;

if ( !current_user_can( 'edit_post', $post->ID ))// - check permissions
return $post->ID;

if( 'test' === $post->post_type ) {// - check post_type
if( 'publish' === $post->post_status ) {// - Just publish

$post_id = $post->ID;
$cats = wp_get_post_categories( $post_id);

foreach ($cats as $cat) {
if ( in_category($cat->slug, $post_id) || $cat->term_id == $cat_id_for_check) { // call for a term slug or id
wp_set_object_terms( $post_id, 'foo', 'post_tag' );
wp_set_object_terms( $post_id, 'bar', 'post_tag' );
}
} // endforeach
} // if publish
} // if post type

} // end function


Luis Abarca comments:

Maybe you can offer some context or the goal for this function, for a better understanding of the problem

2011-09-15

ej_emman answers:

I think you have a wrong syntax to your code
Try to figure out your wp_set_post_categories that was not properly use.
Maybe you just need this:


add_action( 'save_post', 'ifk_set_default_object_terms', 100, 2 );

function ifk_set_default_object_terms() {

global $post;



if ( !current_user_can( 'edit_post', $post->ID ))// - check permissions

return $post->ID;



if( 'test' === $post->post_type ) {// - check post_type
if( 'publish' === $post->post_status ) // - Just publish
{
//call for a term slug or id.

if(in_category('yourfiltercategory', $post->ID))
{

wp_set_object_terms( $post_id, 'foo', 'post_tag' );

wp_set_object_terms( $post_id, 'bar', 'post_tag' );

}


}//end publish

}//end post type

}

2011-09-17

Abdessamad Idrissi answers:

It is hard to understand you question, so I'll just guess some solutions for you :)

* as we can see; you need this when the post is saved, but $cats = wp_set_post_categories( $post); is wrong, better use
<blockquote>wp_set_object_terms( $object_id, $terms, $taxonomy, $append )</blockquote>

* another code that may help:
$categories = $_POST['post_category']; // this shouzld be an array
$cat_ids = array ();
foreach ($categories as $category) {
if ($id = category_exists($category))
$cat_ids[] = $id;
else
if ($id = wp_create_category($category))
$cat_ids[] = $id;
}

if ( $post->ID )
wp_set_post_categories($post->ID, $cat_ids);


* and for conditionals:
if ( is_object_in_taxonomy($post['post_type'], 'category') )
or
if ( is_taxonomy_hierarchical( $taxonomy ) )


hope it helps since the asked question is hard to understand :)