• Resolved Mrskt

    (@mrskt00)


    Hi,

    I created a custom Taxonomy called “genre”

    And Is it possible to add post genres Taxonomy values in post tags meta field automatically after saving/publishing post.

    Means if i added “File Manager” and “Tools” genre in post then the same thing also goes added in post tags field automatically after Publishing/saving post.

    And I also want to prepend “Android” and Append “Download” word in that tags that’ll added from that Genre Taxonomy.

    • This topic was modified 2 years, 7 months ago by Mrskt. Reason: Better Explanation
Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    You can use the ‘set_object_terms’ action to do whatever else you want. This action fires when terms are assigned to an object. You’ll need to do some checks to be sure it’s the right taxonomy and object you are targeting, as this action fires for many different kinds of assignments.

    It seems though you are creating redundant data. That usually shouldn’t be necessary. It’s inefficient to create redundant data, it’s best to make use of single instances of any given data.

    Thread Starter Mrskt

    (@mrskt00)

    @bcworkz Hi, Really Need this for my apk site to Publishing Apps Faster. Also I want learn this magic,so in future i can do this types other work easily. Can you please share exact codes snippets?

    Thread Starter Mrskt

    (@mrskt00)

    @bcworkz I tried this but not working properly if i add multiple Genre in post because all generes collab together and makes a single tag:

    
    add_action('save_post','getags');
    function getags($post_id) {
    	$taxonomy = 'post_tag';
    	$terms = 'Android'.get_the_term_list( $post_id, 'genre').'Download';
     return wp_set_object_terms($post_id, $terms, $taxonomy);
    }
    

    Do you have any solutions?

    • This reply was modified 2 years, 7 months ago by Mrskt.
    Moderator bcworkz

    (@bcworkz)

    Sorry but I’ve no useful snippets to share. You’re kind of halfway there anyway 🙂

    Instead of getting a list of terms, get an array of term slugs with wp_get_post_terms() while using the ‘fields’ arg to only get slugs instead of complete term objects. The returned array can then be passed to wp_set_object_terms(). You can fiddle with the returned slugs before setting object terms to make slugs like “android-my-genre-download”. You can use array_map() to do the same thing to every element in the array. Altered slugs still should maintain proper slug syntax — all lowercase, no special chars other than -.

    If you want more elaborate tags created beyond simple slugs, like a mixed case name, they should be created with wp_insert_term() prior to setting them to an object. In which case you may need more data from the genre terms than just slugs. You could get the full term objects, loop through the objects to create tag terms if they do not already exist, while compiling an array of tag term IDs which can be passed to wp_set_object_terms().

    I’m leery of using the “save_post” action for this. When inserting a new post, it takes time to write assigned terms to the DB. While this writing is happening, PHP continues on, relatively quickly. When your “save_post” callback executes, there might not yet be any terms to get with wp_get_post_terms(). Even worse, this can work fine sometimes, yet fail at others. This is what is known as a “race condition”. Very hard to debug if you’re not aware of what is happening. This is why I suggested ‘set_object_terms’ action.

    Even ‘set_object_terms’ could be problematic if you callback is not passed assigned slugs. If you get IDs instead, you’d need to get the terms from the DB, which still might be in the writing process if it’s a newly inserted term. For existing terms, getting them will work fine.

    Thread Starter Mrskt

    (@mrskt00)

    @bcworkz Below code is working But only one genre is adding in tags field not all genres that i added in my post, Do you know any solution for that?

    
    function getags($post_id) { 
      $genres = get_the_terms( $post_id, 'genre');
      $taxonomy = 'post_tag'; 
      foreach($genres as $genre) {
        $terms = 'Android '.$genre->name.' Download'; 
        wp_set_object_terms($post_id, $terms, $taxonomy); 
      } 
    }
    
    Moderator bcworkz

    (@bcworkz)

    You need
    wp_set_object_terms( $post_id, $terms, $taxonomy, true );
    or the function will clear any existing assigned terms.

    Thread Starter Mrskt

    (@mrskt00)

    Hello @bcworkz , You’re are Brilliant×10000…. My Biggest Problem is solved + I learned New thing…Thank you

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. More lucky than brilliant. I very nearly missed the problem. Just dumb luck that I happened to notice we need to pass true.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Add Post Genre Taxonomy automatically to Post Tags’ is closed to new replies.