Title: Excluding Posts Using a Variable Between Functions
Last modified: August 19, 2016

---

# Excluding Posts Using a Variable Between Functions

 *  [apascal](https://wordpress.org/support/users/apascal/)
 * (@apascal)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/)
 * Hi,
 * I don’t have much knowledge in PHP so I’m not exactly how to do this. I have 
   a Custom Query that displays 5 posts that have a certain meta value (featured
   posts). On the same page, I have a regular old Loop. I would like to exclude 
   the posts displayed in the Custom Query from the Loop. Another problem is that
   the Custom Query and The Loop are in different functions in the functions.php
   file so variable values don’t carry over between the two.
 * Any help would be greatly appreciated!

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

 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470672)
 * You could try something like this in your main loop:
 * <?php
    while (have_posts()) : the_post(); $postmeta = get_post_custom($post->
   ID); if ( $postmeta[‘featured’][0] == ‘yes’ ) continue; _rest of loop code here…_
   endwhile; ?>
 *  Thread Starter [apascal](https://wordpress.org/support/users/apascal/)
 * (@apascal)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470673)
 * Hmm… that didn’t work. After I applied that, nothing showed up at all from the
   Loop. Any other ideas?
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470675)
 * What exactly is the meta value for your featured posts?
 *  Thread Starter [apascal](https://wordpress.org/support/users/apascal/)
 * (@apascal)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470676)
 *     ```
       // 'The Loop' Posts
       function com_post() {
   
       	global $post, $posts, $com_posts, $featured_ID;
   
       	if (have_posts()) : while(have_posts()) : the_post();
   
       		loop code goes here...
   
       	}
   
       	endwhile;
   
       	endif;
   
       }
   
       // Featured Slider Posts
       function com_featured() {
   
       	global $post, $posts, $com_featuredPostsNum;
   
       	$args = array(	'meta_value'=> 'on',
       			'meta_key'=> 'featured_value',
       			'post_type' => 'post',
       			'post_status' => 'publish',
       			'posts_per_page' => $com_featuredPostsNum,
       			'caller_get_posts'=> 1
       		     ); 
   
       	$featured = null; $featured = new WP_Query($args); 
   
       	if ($featured->have_posts()) { 
   
               while ($featured->have_posts()) : $featured->the_post(); $featured_ID = $post->ID; 
   
       		loop code goes here...
   
       	endwhile;
   
       	} wp_reset_query();
   
       }
       ```
   
 * How can I exclude the posts displayed in the featured post query from the general
   Loop? These functions are in functions.php by the way.
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470677)
 * Did you substitute your actual meta key and value into the code to make it work?
 * <?php
    while (have_posts()) : the_post(); $postmeta = get_post_custom($post->
   ID); if ( $postmeta[‘featured_value’][0] == ‘on’ ) continue; rest of loop code
   here … endwhile; ?>
 *  Thread Starter [apascal](https://wordpress.org/support/users/apascal/)
 * (@apascal)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470678)
 * Thanks, it works now! There’s a side-effect to that though. For example, if in
   the Settings it’s set to display 5 entries per page, and 3 of those entries are“
   featured” posts, only 2 entries show up instead of 5 non-featured post showing
   up.
 * I’m sure there’s an easy fix to that. Any help?
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470679)
 * You could put this before the loop to increase the number of posts:
 * query_posts(‘posts_per_page=8’);
 *  Thread Starter [apascal](https://wordpress.org/support/users/apascal/)
 * (@apascal)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470680)
 * Well, the thing is, this a theme that will be sold so as a developer, I don’t
   know how many posts will be set to be displayed on the homepage. That being said,
   I also don’t know how many featured posts will be set, and of the number that
   is set, how many will actually have the “featured” meta value.
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470681)
 * Unfortunately, I cannot think of an easy solution off-hand (which is not to say
   there might not be one).
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470682)
 * Maybe the best solution is to forget everything I said, and use WP_Query to create
   a custom main loop that only includes non-featured posts.
 *  Thread Starter [apascal](https://wordpress.org/support/users/apascal/)
 * (@apascal)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470803)
 * How would I do that? I can just do a WP_Query that lists posts that **don’t**
   have the meta value but then if in the database there are more “featured” posts
   than what the user specifies to be displayed in the slider, they will just not
   appear anywhere.

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

The topic ‘Excluding Posts Using a Variable Between Functions’ is closed to new 
replies.

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [exclude](https://wordpress.org/support/topic-tag/exclude/)
 * [featured](https://wordpress.org/support/topic-tag/featured/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 2 participants
 * Last reply from: [apascal](https://wordpress.org/support/users/apascal/)
 * Last activity: [16 years, 1 month ago](https://wordpress.org/support/topic/excluding-posts-using-a-variable-between-functions/#post-1470803)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
