0
votes

My Wordpress front page has both a featured article (styled inside a box) and a list of recent posts (styled separately). I want to display these recent posts using Wordpress loop excluding the featured post. It is easy to achieve excluding a certain category or a tag, but in my case I want to exclude a post with a custom field. The featured post has a custom field with a name and a value of: featured = yes.

How do I achieve this without using a plugin?

2

2 Answers

2
votes

You can use meta_query parameter, as explained in http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

Something like:

$args = array(
    'post_type' => 'any',
    'meta_query' => array(
        array(
            'key' => 'featured',
            'value' => 'yes',
            'compare' => 'NOT LIKE'
        )
    )
);

$query = new WP_Query( $args );
0
votes
$args = array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'featured',
            'value' => 'yes',
            'compare' => '='
        ),
    ));
$ids = array();
$query = new WP_Query($args); // fetching posts having featured = yes
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $ids[] = $post->ID; // building array of post ids
    }
}

$args = array( 'post__not_in' =>$ids); // excluding featured posts from loop
query_posts($args);

while (have_posts()) : the_post();
// rest of the code