Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In a category.php template, I built a function that lists "featured" posts before the main loop. The featured posts are selected using a custom field and get_posts():

function list_featured_articles(){
 $featured_posts = get_posts('meta_key=featured_article&meta_value=on&numberposts=10&order=DESC&orderby=post_date');
}

My category.php template:

echo list_featured_articles();
get_template_part( 'loop', 'category' );

It works fine, except that the featured articles are listed also in the category list of articles. I would like, from my function, get the featured posts' ID and remove them from the main loop. Is there a proper way to do it? I figure i could use a GLOBAL variable storing an array of excluded ids, but i'd like to avoid using Globals for that.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

This would be an appropriate use of query_posts(), with a post custom meta query.

Since you're querying by meta_key=featured_article&meta_value=on, you would then exclude on the same parameters.

<?php
// Setup the custom meta-query args
$exclude_featured_args = array(
    'meta_query' => array(
        array(
            'key' => 'featured_article',
            'value' => 'on',
            'compare' => '!='
        )
    )
 );
// globalize $wp_query
global $wp_query;
// Merge custom query with $wp_query
$merged_args = array_merge( $wp_query->query, $exclude_featured_args );
// Query posts using the modified arguments
query_posts( $merged_args );
?>

That should exclude featured posts from the main loop.

Note: you'll only want to do this in the same context in which you display the featured posts loop.

EDIT

From your comment:

my function is set up so that if there are no "featured" post, it automatically takes the most recent ones and display them as "featured"

Again, you can take whatever method you use to include posts in your featured loop, and then use the same arguments to exclude the same posts from the primary Loop.

Without knowing what your method is, I can't give a precise answer for how to incorporate it into your excluded-posts argument array.

share|improve this answer
    
this sounds good, but would this makes an additional sql query or would it replace the initial "main" query? –  pixeline Oct 29 '11 at 21:43
1  
You should read the Codex link for query_posts(), as it answers this question. The query_posts() function is used to alter the primary Loop. –  Chip Bennett Oct 29 '11 at 21:44
    
Forgive me for not mentioning it directly: my function is set up so that if there are no "featured" post, it automatically takes the most recent ones and display them as "featured". So i can't really take that road (although i'm still interested to know if your answer replaces the query or creates an additional one). –  pixeline Oct 29 '11 at 21:47
    
ok i got it: i adapted your proposal: In my function i stored the IDs in a global variable. I then use it in my category.php template like this, before calling the loop: $exclude_featured_args = array( 'post__not_in' => $excluded_posts ); $merged_args = array_merge( $wp_query->query, $exclude_featured_args ); query_posts( $merged_args ); –  pixeline Oct 29 '11 at 21:51
    
"although i'm still interested to know if your answer replaces the query or creates an additional one"... from my above comment: The query_posts() function is used to alter the primary Loop. It alters the primary Loop; it does not create a new loop. –  Chip Bennett Oct 29 '11 at 21:53
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.