Title: Passing array data to a variable
Last modified: August 7, 2018

---

# Passing array data to a variable

 *  [Galaxy_High](https://wordpress.org/support/users/galaxy_high/)
 * (@galaxy_high)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/passing-array-data-to-a-variable/)
 * How do I pass the contents of $the_slug array to the $post_name variable in the
   loop?
 *     ```
       $tags = get_the_tags();
       foreach ($tags as $tag){
   
        global $post;
        $the_slug = $tag->slug; //contains 10ish words that associate with my permalinks: welcome, home, about, contact, etc
   
        $post_id = 'welcome';
        $post_name = $the_slug; //fails to populate here
        $queried_post = get_post($post_name); //if changed to $post_id works but only 'welcome' post
        $excerpt = $queried_post->post_excerpt;
        $excerpt = apply_filters('the_content', $excerpt);
        $excerpt = str_replace(']]>', ']]>', $excerpt);
        echo $excerpt . "\n\n";
        }
       ```
   
 * Thanx for looking,
    Sam
    -  This topic was modified 7 years, 9 months ago by [Galaxy_High](https://wordpress.org/support/users/galaxy_high/).
      Reason: Formatting

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

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/passing-array-data-to-a-variable/#post-10567739)
 * This looks very similar to your other question.
    You need to look up the functions
   you are using in the reference, so that you get the parameters correct. [http://developer.wordpress.org/reference](http://developer.wordpress.org/reference)
 *  Thread Starter [Galaxy_High](https://wordpress.org/support/users/galaxy_high/)
 * (@galaxy_high)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/passing-array-data-to-a-variable/#post-10568001)
 * Yep, I’ve got so far and don’t understand why I can’t pass an array containing
   strings to a variable using a loop. That’s why I’m asking for help.
    You might
   as well said, “The answers are here: [google.com](http://www.google.com)“
 *  [tugbucket](https://wordpress.org/support/users/tugbucket/)
 * (@tugbucket)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/passing-array-data-to-a-variable/#post-10576105)
 * To answer (I think) your need:
 * > ” don’t understand why I can’t pass an array containing strings to a variable
   > using a loop”
 *     ```
       global $post;
       $tags = get_the_tags();
       if($tags){
       	$post_name = '';
       	foreach ($tags as $tag){
       		$the_slug = $tag->slug;
       		$post_name .= $the_slug; 
       	}
       }
       echo $post_name;
       ```
   
 * but looking at your code, I can’t quite figure out what you are trying to accomplish.
   What exactly is your end result wanted to be?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/passing-array-data-to-a-variable/#post-10576359)
 * I think your problem is the use of get_post(). You cannot use this function to
   get posts by title or slug. Only an ID or a complete post object will work. To
   get a post by title, you can use get_page_by_title(). This is obviously meant
   for pages, but you can pass different post types to get regular posts or custom
   posts types. This function generates a SQL query to get posts by post_title, 
   not slug (post_name). Thus in your case, you should get the tag name to match
   against post titles, not the tag slug. This is assuming tag names and post titles
   do indeed correlate, since matching slugs does not mean matching titles.
 *     ```
        $the_name = $tag->name;
   
        $post_title = $the_name;
        $queried_post = get_page_by_title( $post_title, OBJECT, 'post'); //gets _post_ by title
       ```
   
 * Going back to tugbucket’s statement in your other topic, this seems very convoluted
   since tags have their own description field you can use to output what is currently
   in post excerpts. This would be way more expedient and efficient, requiring many
   fewer SQL queries be made. If the volume of excerpts that would need to be copied
   to tag descriptions is what is holding you back, a one time script to automate
   the process wouldn’t be too difficult to develop. It wouldn’t be a lot different
   than the code you have now. You get all tags instead of post tags, and then get
   the related post excerpt like I did above; and finally, set the tag description
   instead of outputting the excerpt.
 *  [Akash Dixit](https://wordpress.org/support/users/akashdixit1/)
 * (@akashdixit1)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/passing-array-data-to-a-variable/#post-10581084)
 * I think this code will help you
    global $get; $arr= get_the_tags(); if($arr) {
   $get_name= ”; foreach($arr as $ar){ $the_slug = $ar->slug; $get_name .= $the_slug;}}
   echo $get_name;

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

The topic ‘Passing array data to a variable’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 5 participants
 * Last reply from: [Akash Dixit](https://wordpress.org/support/users/akashdixit1/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/passing-array-data-to-a-variable/#post-10581084)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
