0

I need to run a query (via functions.php) and find all posts with a certain tag and add those posts to a category.

example:

Find all posts with tag "car" and add them to category "transportation".

EDIT: STILL NOT WORKING, BUT...

This is what I have so far thanks to @benoti's answer (bellow):

$args = array(
        'post_type'=>'post',
        'tax_query' => array(
        'taxonomy' => 'tag',
        'field'    => 'slug',
        'terms'    => 'car',
    ),
);
$posts = get_posts($args);
        foreach ($posts as $post) :
  //do stuff 
   $cat_id = 1669; // the ID of category transportation
   $append = true; // If true, terms will be appended to the object. If false, terms will replace existing terms
    // make some verif that's better
    wp_set_object_terms($post->ID, $cat_id, 'category', $append);
     endforeach;

...but it's not working.

Also, to clarify:

I need to add a category to all posts that contain the "car " tag, but I do not know in what category they (the posts) are in.

How can I safely do this without break my site?

1

This is just an example to get all posts in the category transportation with the tag car :

$args = array(
'post_type'=>'post',
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'category',
        'field'    => 'slug',
        'terms'    => array( 'transportation' ),
    ),
    array(
        'taxonomy' => 'tag',
        'field'    => 'slug',
        'terms'    => array( 'car' ),
        'operator' => 'IN',
    ),
);
$posts = get_posts($args);

EDIT : for a simple tax_query

'tax_query' => array(
    array(
        'taxonomy' => 'tag',
        'field' => 'slug',
        'terms' => 'car'
    )
)

You can grab more details and it for your case here https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

This will not get the posts tag 'car' and add category 'transportation' to them, but you need first to get posts. With this result you can loop through it and use wp_set_object_terms( $object_id, $terms, $taxonomy, $append ); in this loop to add your category to the tagged posts.

foreach($posts as $post){
    $append = true; // If true, terms will be appended to the object. If false, terms will replace existing terms
    // make some verif that's better
    wp_set_object_terms($post->ID, 'transportation', 'category', $append);
}

the best it to read https://codex.wordpress.org/Function_Reference/wp_set_object_terms

I hope you get it !

  • This doesn't answer his question. It does not add there items to a category. – Fencer04 Sep 29 '16 at 15:13
  • @Fencer04 : I edit my answer with the way to do it. I let you improve it if you have time. – Benoti Sep 29 '16 at 16:05
  • Thanks @Benoti! But that doesn't help me much... The big issue form me is precisely how to add category to each one after I find all posts with the tag... – bpy Sep 29 '16 at 16:49
  • @Alex : I add what you want – Benoti Sep 29 '16 at 16:56
  • @Benoti, can you please see my edit above? – bpy Sep 29 '16 at 18:51
0

OK! Thanks to @Benoti I was able to find a way of doing this.

It's possible that it can be improved but you can always contribute...

$args = array('post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => 'tag_slug_to_find',// <= your cat slug to find
        ),
    ),
);
$posts = get_posts( $args );
        foreach ($posts as $post) :
   // let's do some stuff 
   $cat_id = 'cat_slug_name'; //Cat slug to append to the object
   // If true, terms will be appended to the object. If false, terms will replace existing terms
    $append = true; 
   // make some verif that's better
   $term_taxonomy_ids = wp_set_object_terms($post->ID, $cat_id, 'category', $append);
   // Let's count how many object's where changed 
   // (If you know a better way, please let us know. Thanks!)
   $count = count( wp_set_object_terms($post->ID, $cat_id, 'category', $append)); 

    if ( is_wp_error( $term_taxonomy_ids ) ) {
    echo 'There his an error somewhere and the terms could not be set.';
} else {
    echo ''. $count . ' posts with tag "'.$cat_id.'" where changed.';
}
     endforeach;

Thank you again, @Benoti! Your answer was very useful!

  • $count($term_taxonomy_ids) would be better, wp_set_object_terms return an array of the term affected. In your case, you must count and increment a value (you don't need to count, increment just +1 on the else statement), so if wp_set_object_terms is ok use, $i++ . – Benoti Oct 1 '16 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.