0
votes

As per the image below - I have a blog archive page which shows a featured blog post as a large one and then shows all of the blog posts below that.

enter image description here

I would like to add something to the main blog loop to say:

IF the blog blog post is featured, don't display it here

So I can stop duplicate blog posts showing up (one featured and then the same one in the main blog loop).

I have two bits of code, one that pulls the featured post through and one that loops all of the posts through. Here

<!-- Featured Blog Item -->

<?php
$loop = new WP_Query( array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'meta_key'      => 'featured',
      'meta_value'  => 'yes'
  )
);
?>

<div class="container">
<div class="row no-gutters">

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <div class="col-12">

        <a href="<?php the_permalink(); ?>">
        <div class="hero__overlay featured-grad-blog">
        </div>
        </a>

        <div class="featured-blog-container">
            <h3><?php the_title(); ?></h3>
            <p><?php the_date(); ?></p>
        </div>
        <?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
        <div class="featured-blog-grid-image-container" style="background-image:url(<?php echo $feat_image; ?>);"></div>


    </div>


<?php endwhile; wp_reset_query(); ?>

</div>
</div>



<!-- All Blog Items -->

<?php
$loop = new WP_Query( array(
    'post_type' => 'post',
    'posts_per_page' => -1
  )
);
?>

<div class="container blog-page-container">
  <div class="row blog-page-row">

  <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>


      <div class="col-lg-4 col-sm-6 col-xs-12 blog-page-col">



        <div class="blog-image" style="position: relative;">
          <a href="<?php the_permalink(); ?>">
            <div class="hero__overlay grad-blog-hover"></div>
          </a>
          <?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
          <div class="blog-grid-image-container blog-page-image blog-post-image-div" style="background-image:url(<?php echo $feat_image; ?>);"></div>
        </div>

        <div class="blog-title">
          <a href="<?php the_permalink(); ?>">
            <h4><?php the_title(); ?></h4>
          </a>
        </div>

        <div class="blog-bars-outside-container">
          <div class="blog-bars-inside-container">
            <div class="blog-all-bar blog-bar1"></div>
            <div class="blog-all-bar blog-bar2"></div>
            <div class="blog-all-bar blog-bar3"></div>
            <div class="blog-all-bar blog-bar4"></div>
            <div class="blog-all-bar blog-bar5"></div>
          </div>
        </div>

        <div class="blog-excerpt">
         <?php the_excerpt(); ?>
        </div>

        <div class="meta-details">
          <p><?php the_author_meta( 'display_name', $author_id ); ?> - <?php echo get_the_date(); ?></p>
        </div>

      </div>


  <?php endwhile; wp_reset_query(); ?>

  </div>
</div>

I guess I would need to add the opposite of this maybe, into the array for the main blog posts:

'meta_key'      => 'featured',
'meta_value'    => 'yes'

But I can't get anything to work...

Any help would be greatly appreciated!

1
How are you setting that "featured" meta data? I'm assuming that is a custom field. Why don't you just use the built in sticky posts instead?disinfor
Yes its actually a custom field - Are sticky posts a built in Wordpress feature? or do I need a separate plugin?Shaun Taylor
Yes, sticky posts are a built in WP feature. It looks like you got the answer you were looking for, but here's some docs on using sticky posts: developer.wordpress.org/themes/functionality/sticky-postsdisinfor
Thanks so much, Its worth me looking at that for the next site I build no doubt :)Shaun Taylor

1 Answers

1
votes

For the non-featured part, please try this:


<!-- All Blog Items -->

<?php
        $loop = new WP_Query( array(
                'post_type' => 'post',
                'posts_per_page' => -1,
                'meta_query' => array(
                    array(
                        'key' => 'featured',
                        'value' => 'yes',
                        'compare' => '!='
                    )
                )
            )
        );
?>

This should select all posts where 'featured' is not equal '!=' 'yes'.