Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6915885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:31:52+00:00 2026-05-27T09:31:52+00:00

UPDATE, the code is working now. I have the following code but obliviously it

  • 0

UPDATE, the code is working now.

I have the following code but obliviously it doesn’t work as expected. It should take the terms from the taxonomy TAXONOMY_NAME only for custom post type CUSTOM_POST_TYPE and add as tags.


add_action('save_post','add_tags_auto');
function add_tags_auto($id) {

    $terms = get_the_terms( $post->id, 'TAXONOMY_NAME' ); // get an array of all the terms as objects.
    $add_tags = array();

    foreach( $terms as $term ) {
        $add_tags[] = $term->slug; // save the slugs in an array
    }
    $temp = array();
    $tags = get_the_tags($id);
    if ($tags) {
    foreach ($tags as $tag)
        $temp[] = $tag->name;
    }
    $tags = $temp;

    $post = get_post($id);

    if ($post->post_type != 'CUSTOM_POST_TYPE')
        return false;

    foreach ($add_tags as $t)
        if (!in_array($t,$tags))
            wp_add_post_tags($id,$add_tags);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:31:52+00:00Added an answer on May 27, 2026 at 9:31 am

    This is untested, but it should be a step in the right direction, I’ve made several tweaks and will comment:

    <?php
    add_action('save_post','add_tags_auto');
    function add_tags_auto($post_id) {
        $post = get_post($post_id);
    
        // bail if this isn't a type of post we want to auto-tag
        if($post->post_type != 'CUSTOM_POST_TYPE')
            return false;
    
        // --------------------------------------------------
        // get the list of tag names from the post
        // --------------------------------------------------
        $tagNames = array();
        $tags     = get_the_tags($post_id);
    
        foreach($tags as $tag)
            $tagNames[] = $tag->name;
    
        // --------------------------------------------------
        // get the list slugs from terms in the post, any
        // slugs that aren't alredy a tag, will be marked for
        // addition automatically
        // --------------------------------------------------
        $tagsToAdd = array();
        foreach(get_the_terms($post_id, 'TAXONOMY_NAME') as $term)
            if(!in_array($term->slug, $tagNames))
                $tagsToAdd[] = $term->slug;
    
        // if we have some new tags to add, let's do that now
        if(!empty($tagsToAdd))
            wp_add_post_tags($post_id, implode(',', $tagsToAdd));
    }
    

    One potential problem I saw with your code was setting $post after trying to use it. I’m pretty sure when $post->id is called on the first line of your function PHP is raising a warning that $post is a non-object. So I’ve moved the call to get_post up to the top of the function.

    A little tweak I added is to return early if the post is not the desired type. Better to do this ASAP, rather than spend time doing calculations that may just be discarded by the check further down the line.

    I’ve also consolidated the number of foreach loops and renamed some of the variables for clarification. The only other thing I saw in your original code that may not have been working as expected was the call to wp_add_post_tags, it’s documented to take a comma delimited string as the second parameter, not an array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which was working but now for some reason does
So far i have the following code, but it doesn't seem to be working,
I have the following code: UPDATE myTable SET Col1 = @Value However, I have
I have implement the following code in order to test playing a video from
The code seems not working. // $counter is an instance of Zend_Db_Table_Abstract $counter->update(array('hits' =>
Update: Solved, with code I got it working, see my answer below for the
I am trying to update code via Linq, but I am getting this error:
I have a website that I regularly update the code to. I keep it
I'm running the following code to update the database according to the data I
I have been working on learning C++ and Qt4 recently, but I have hit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.