• Hi,

    I presume it’ll be hard to manage but, I wonder if it was possible to add some taxonomies terms to a post depending on what we enter in a custom field value ? The fact is, I actually use a custom taxonomy to sort my custom posts type by french code postal.

    My client want a field where we can enter the department number which is composed by two digits in my country (e.g. : 67) and when we save the new post, it automatically attributes to this post all the taxonomies terms that begin with thoses digits (e.g. : for ’67’ : 67500, 67150, 67200, 67800 …).

    I’m a begginer in wordpress development and I don’t know at all how to make that…
    I presume we need to go through a custom plugin ?

    Thank you for your help

    • This topic was modified 7 years, 10 months ago by Nox.
    • This topic was modified 7 years, 10 months ago by Nox.
    • This topic was modified 7 years, 10 months ago by Nox.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Nox

    (@profnox)

    I thought about something like that, but I presume it will not work. EDIT : It doesn’t.

    
    /*
    Plugin Name: CP from Departement
    Description: Set CP depending on departement
    Author: ProfNox
    */
    
    add_action( 'save_post', 'add_postal_code', 10, 3 );
    
    function add_postal_code( $post_ID, $post, $update ) {
    	$departement = get_post_meta($post_ID, 'departement', true);
    	if ($departement != null){
    		$terms = [];
    		$all_cp = get_terms( array(
    			'taxonomy' => 'cp',
    			'hide_empty' => false,
    		) );
    		foreach($all_cp as $key => $value){
    			if (substr( $value->name, 0, 2 ) == $departement)
    				array_push($terms, $value); 
    		}
    		wp_set_object_terms($post_ID, $terms, 'cp'); 
    	}
    }

    EDIT : got this error : Warning: trim() expects parameter 1 to be string, object given in C:\wamp\www\wordpress2\wp-includes\taxonomy.php on line 2238

    I’m totally lost it’s only my second try on plugin development…

    • This reply was modified 7 years, 10 months ago by Nox.
    • This reply was modified 7 years, 10 months ago by Nox.
    • This reply was modified 7 years, 10 months ago by Nox.
    Moderator bcworkz

    (@bcworkz)

    That error is from the declaration for wp_set_object_terms(), so the problem must be in the parameters you supply to that function. $post_ID ought to be OK, it came from the action hook. ‘cp’ ought to be OK, that’s certainly your taxonomy. It must be $terms. If you were to print_r( $terms ); you would see you have an array of term objects. wp_set_object_terms() wants an array of IDs or slugs, not objects!

    Instead of pushing $value to build the array, push $value->name or $value->term_id.

    Thread Starter Nox

    (@profnox)

    Oh, my bad. Indeed, it works fine now.

    Thank you for your help, it was not so hard finally, I just need to learn to read ! …

    Moderator bcworkz

    (@bcworkz)

    ?? That’s my trick! You’re welcome.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Automaticaly add taxonomy’s terms depending on a field value’ is closed to new replies.