Title: wp cli post_tag spaces
Last modified: February 6, 2017

---

# wp cli post_tag spaces

 *  [DeathMarine](https://wordpress.org/support/users/deathmarine/)
 * (@deathmarine)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/)
 * how do i use post_tag if the tag i want has spaces?
 * in wordpress for example the post tag “x and y” will show up as “x and y”
 * in wp cli it will show up as “x” “and” “y”. it’s three tags instead of one
 * or if you know a good way to get around this behavior, I would also like to hear
   suggestions
 * thanks in advance!

Viewing 15 replies - 1 through 15 (of 15 total)

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8743050)
 * Are you using this command?
    [http://wp-cli.org/commands/term/create/](http://wp-cli.org/commands/term/create/)
 *  Thread Starter [DeathMarine](https://wordpress.org/support/users/deathmarine/)
 * (@deathmarine)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8743079)
 * I’m using this command to add them to an existing post
 * wp post term add post_id post_tag $var1
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8743133)
 * Try adding quotes around the $var1 variable.
 *     ```
       wp post term add 123 post_tag "$var1"
       ```
   
    -  This reply was modified 9 years, 4 months ago by [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/).
 *  Thread Starter [DeathMarine](https://wordpress.org/support/users/deathmarine/)
 * (@deathmarine)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8743261)
 * Ah, I see. It automatically replaces the spaces with dashes.
 * I was hoping for to keep the spaces in the tag but i’ll work with it.
 * thank you.
 * Also, is there a limit to the amount of tags in a string i can use? I’ve accidentally
   loaded a tag with lots of words and it didn’t automatically add dashes, it kept
   the spaces and made 1 long tag. Could that mean there is a way to stop the auto
   dash behavior?
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8743410)
 * > Ah, I see. It automatically replaces the spaces with dashes.
 * It doesn’t for me. But It does for the slugs of the terms.
 *     ```
       +---------+---------------+--------------+----------+
       | term_id | name          | slug         | taxonomy |
       +---------+---------------+--------------+----------+
       | 191     | x and y       | x-and-y      | post_tag |
       | 192     | x and y,z     | x-and-yz     | post_tag |
       | 193     | x,y           | xy           | post_tag |
       +---------+---------------+--------------+----------+
       ```
   
 * Try the list command to see what terms belong to a post (with id 123)
 *     ```
       wp post term list 123 post_tag
       ```
   
 * Without looking at the rest of the code it’s difficult to say what’s going wrong.
    -  This reply was modified 9 years, 4 months ago by [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/).
 *  Thread Starter [DeathMarine](https://wordpress.org/support/users/deathmarine/)
 * (@deathmarine)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8746617)
 * reformated the post
    -  This reply was modified 9 years, 4 months ago by [DeathMarine](https://wordpress.org/support/users/deathmarine/).
      Reason: bad formatting
 *  Thread Starter [DeathMarine](https://wordpress.org/support/users/deathmarine/)
 * (@deathmarine)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8746619)
 * I use the following to read a list of terms
 *     ```
       for($i=0; $i<=20; $i++){
       	$tags[$i] = $xpath->evaluate("string(//div[contains(@class, 'main_genre')]/a[$x])");
   
       	if(empty($tags[$i])){
       		array_pop($tags);
       		break;
       	}
   
       	echo $tags[$i] . "\n";
       	$x++;
       }
       ```
   
 * Then i add them as tags into a wordpress post.
 *     ```
       $length = sizeof($tags);
       for ($i=0; $i<$length; $i++)
       {
       	shell_exec("wp post term --allow-root add $int post_tag '$tags[$i]'");
       }
       ```
   
 * Not sure why it would automatically replace spaces with dashes since you said
   it shouldn’t have that behavior.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8748442)
 * Check if it works by replacing the dashes before adding the term.
 *     ```
       $tag = str_replace( '-', ' ', $tags[ $i ] );
       shell_exec("wp post term --allow-root add 1 post_tag '$tag'");
       ```
   
 * If it does you know wp-cli does insert them correctly and the `$tags` variable
   contains tag names with dashes.
 * Why not use [wp_insert_term()](https://developer.wordpress.org/reference/functions/wp_insert_term/)
   or [wp_set_object_terms()](https://developer.wordpress.org/reference/functions/wp_set_object_terms/)
   to add the terms?
    -  This reply was modified 9 years, 4 months ago by [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/).
 *  Thread Starter [DeathMarine](https://wordpress.org/support/users/deathmarine/)
 * (@deathmarine)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8774848)
 * Unfortunately i still get the dashes.
 * I’m a novice so i just went with the easiest option i found. Emphasis on “found”
   😛
 * I just happen to run acrross this command which seems to be exactly what i need:
   `
   wp_set_post_terms( $post_id, $terms, $taxonomy, $append )`
 * But i can’t get it to work properly. I tried:
    `shell_exec("wp_set_post_terms('
   $int', '$tags', 'post_tag', 'true' )");` to dump the entire array and got:
 *     ```
       PHP Notice:  Array to string conversion in *directory*/test.php on line 60
       sh: 1: Syntax error: word unexpected (expecting ")")
       ```
   
 * then tried:
 *     ```
       $length = sizeof($tags);
       for ($i=0; $i<$length; $i++)
       {
       	shell_exec("wp_set_post_terms( '$int', '$tags[$i]', 'post_tag', 'true' )");
       }
       ```
   
 * and got the following for each element in the array:
    `sh: 1: Syntax error: word
   unexpected (expecting ")")`
 * but then i though, “it’s not command line code im calling” so i tried the to 
   use the function by itself without the shell_exec and got:
    `PHP Fatal error:
   Uncaught Error: Call to undefined function wp_set_post_terms()`
 * Can you point out what i’m doing wrong?
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8782954)
 * If you want to use `wp_set_post_terms()` WordPress needs to be loaded. If you’re
   using it in a plugin or theme that would work.
 * Where are you running this script? And what is it you want it to do?
    -  This reply was modified 9 years, 4 months ago by [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/).
 *  Thread Starter [DeathMarine](https://wordpress.org/support/users/deathmarine/)
 * (@deathmarine)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8788712)
 * It’s a php file i run from command line.
 * It gets some info from a website, creates a post using wp-cli, puts the info 
   on it.
 * It works. I just didn’t want dashes in my post tags.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8793759)
 * I can’t replicate this issue locally when adding tags with spaces using the `
   wp post term` command.
 * It could be shell_exec adds these dashes. Try escaping the variable.
 *     ```
       $tag = escapeshellarg( $tags[$i] );			
       shell_exec("wp post term --allow-root add 1 post_tag $tag");
       ```
   
 *  Thread Starter [DeathMarine](https://wordpress.org/support/users/deathmarine/)
 * (@deathmarine)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8794898)
 * Unfortunately that didn’t work.
 * if it makes a difference, the tags are coming from a UTF-8 encoded webpage.
 * I put an echo command right before the shell_exec command so it is being fed 
   a tag with a space like ‘this tag’ and when it’s posted its still ‘this-tag’
 * tried this:
 *     ```
       $tag = escapeshellarg( $tags[$i] );			
       shell_exec("wp post term --allow-root add 1 post_tag $tag");
       ```
   
 * and
 *     ```
       $tag = escapeshellarg( $tags[$i] );			
       shell_exec("wp post term --allow-root add 1 post_tag '$tag'");
       ```
   
 * I think you are right about shell_exec. I ran in command line just this line
   `
   wp post term --allow-root add 1 post_tag 'this tag'` and it adds it to the post
   without the dash.
 * what do you recommend to do? Should I have created a Bash file instead to do 
   this task? Should I load enough wordpress to be able to run stuff like what i
   tried above with “wp_set_post_terms()”?
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8798438)
 * Take a look at extending WP-CLI with your own command.
    [http://wp-cli.org/#extending](http://wp-cli.org/#extending)
   [https://make.wordpress.org/cli/handbook/commands-cookbook/](https://make.wordpress.org/cli/handbook/commands-cookbook/)
 * With that you can make use of the native WP functions.
 *  Thread Starter [DeathMarine](https://wordpress.org/support/users/deathmarine/)
 * (@deathmarine)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8801959)
 * Thanks!
 * I greatly appreciated your time!

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘wp cli post_tag spaces’ is closed to new replies.

## Tags

 * [wp-cli](https://wordpress.org/support/topic-tag/wp-cli/)

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 15 replies
 * 2 participants
 * Last reply from: [DeathMarine](https://wordpress.org/support/users/deathmarine/)
 * Last activity: [9 years, 3 months ago](https://wordpress.org/support/topic/wp-cli-post_tag-spaces/#post-8801959)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
