Title: Adding External Variable to An Array
Last modified: August 20, 2016

---

# Adding External Variable to An Array

 *  [southsideslim03](https://wordpress.org/support/users/southsideslim03/)
 * (@southsideslim03)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/adding-external-variable-to-an-array/)
 * Here is what I’m trying to accomplish:
 * $cat_id = get_post_meta($post_id, ‘category’, ‘true’);
 * $args = array( ‘numberposts’ => 5,’category’ => $cat_id );
    $myposts = get_posts(
   $args );
 * In layman’s terms, I want to pull the value of the category ID from a post custom
   field. Then add that field into the argument array to dynamically display posts
   in the sidebar on a page depending on what category ID they used in the post.
   Each time I do this it displays all of my posts and then the last post is displayed
   as content in the main section of my site. Should $cat_id be initialized a different
   way?

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

 *  Thread Starter [southsideslim03](https://wordpress.org/support/users/southsideslim03/)
 * (@southsideslim03)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/adding-external-variable-to-an-array/#post-2364301)
 * I think I could potentially do something like the following:
 * $args[‘category’] = get_post_meta($post_id, ‘category’, ‘true’);
 * Just wondering how to get around the last post showing up in the main content
   area.
 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/adding-external-variable-to-an-array/#post-2364355)
 * Is there a reason for storing the category ID in a custom field?
 *     ```
       $categories = get_the_category( $post_id );
   
       foreach( $categories as $category ) {
           $cat_ids[] = $category->cat_ID;
       }
   
       $args = array(
           'numberposts' => 5,
           'category__in' => $cat_ids
       );
   
       $myposts = get_posts( $args );
       ```
   
 * > Each time I do this it displays all of my posts and then the last post is displayed
   > as content in the main section of my site.
 * What exactly is happening?
 *  Thread Starter [southsideslim03](https://wordpress.org/support/users/southsideslim03/)
 * (@southsideslim03)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/adding-external-variable-to-an-array/#post-2364359)
 * I’m trying to show the 5 most recent posts for a category that is related to 
   a certain post. (I’m using WP-Property so if you understand that then: I’m trying
   to display the 5 most recent posts related to a property type called neighbhorhood)
 * so $categories = get_the_category( $post_id ); will not work in this case because
   1) this is a custom post type that can’t be subjected to categories and 2) trying
   to make this as user-friendly as possible without adding too much custom code.
 * What exactly is happening is that all the original content is replaced with the
   actual post that is the last post in the 5 most recent posts in my sidebar.
 *  Thread Starter [southsideslim03](https://wordpress.org/support/users/southsideslim03/)
 * (@southsideslim03)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/adding-external-variable-to-an-array/#post-2364377)
 * Wait…this might work:
 *     ```
       $cat_id = get_post_meta($post_id, 'category', 'true');
       foreach( $cat_id as $category ) {
           if $cat_id = $category->slug;
       $args = array( 'numberposts' => 5,'category' => $category-> slug);
       else //do something to loop through again
       }
       endforeach;
       ```
   
 * Basically input the category slug in a custom field, grab the value, compare 
   against all of the category slugs, if it matches set the cateogy slug in the 
   $args array, and then run the `$myposts = get_posts($args);` function…
 * I think that would work…just not sure how to get out of the if statement when
   the category doesn’t match.
 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/adding-external-variable-to-an-array/#post-2364385)
 * Your last bit of code is a bit wonky. What you probably want it is this:
 *     ```
       $cat_name = get_post_meta( $post_id, 'category', 'true' );
       $cat_id = ( ! empty( $cat_name ) ) ? get_cat_ID( $cat_name ) : false;
   
       if ( $cat_id ) {
           $myposts = get_posts( array( 'numberposts' => 5, 'category__in' => $cat_id ) );
   
           /* Anything else when the category exists */
       } else {
           /* Anything when the category doesn't exist */
       }
       ```
   
 * Then you can set the custom field to your category name.
 * Categories can be used with custom post types if they’re enabled when the post
   type is registered and I would think categories would be more user friendly than
   custom fields. Using a custom field is fine though. I’m not sure what’s happening
   with regard to the last post as I don’t know how you’re implementing things or
   what exactly is messing up. If you have an example link that might be helpful.
 *  Thread Starter [southsideslim03](https://wordpress.org/support/users/southsideslim03/)
 * (@southsideslim03)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/adding-external-variable-to-an-array/#post-2364410)
 * Big Bagel,
 * Tried your code above:
 *     ```
       <?php if ( get_post_meta($post->ID, 'category', true) ) : ?>
                        	<?php
       					$cat_name = get_post_meta( $post_id, 'category', 'true' );
                           $cat_id = ( ! empty( $cat_name ) ) ? get_cat_ID( $cat_name ) : false;
   
                           if ( $cat_id ) {
                               $myposts = get_posts( array( 'numberposts' => 5, 'category__in' => $cat_id ) );
                           	echo 'sucks2';
                               /* Anything else when the category exists */
                           } else {
                               /* Anything when the category doesn't exist */
       						echo 'sucks';
                           }
       					?>
                       <?php endif;?>
       ```
   
 * Result is echoing sucks…anything I’m doing wrong? Custom Field category is set
   to the name of the category and not the slug.
 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/adding-external-variable-to-an-array/#post-2364453)
 * Is `$post_id` set to anything? I assumed it was a variable you had set to the
   ID of your post earlier in the code. Try this instead:
 *     ```
       <?php
       $cat_name = get_post_meta( $post->ID, 'category', 'true' );
       $cat_id = ( ! empty( $cat_name ) ) ? get_cat_ID( $cat_name ) : false;
   
       if ( $cat_id ) {
           $myposts = get_posts( array( 'numberposts' => 5, 'category__in' => $cat_id ) );
           echo 'sucks2';
           /* Anything else when the category exists */
       } else {
           echo 'sucks';
           /* Anything else when the category doesn't exist */
       }
       ?>
       ```
   
 * You don’t really need the outer if-statement as the code accounts for `get_post_meta()`
   returning nothing.

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

The topic ‘Adding External Variable to An Array’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 7 replies
 * 2 participants
 * Last reply from: [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/adding-external-variable-to-an-array/#post-2364453)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
