0

I followed the guidelines of this article to create a featured post, but it displayed two similar posts: 1 featured post and 1 new post. I want to hide that article when it is used as a featured post. What should I do? Here is the code that I use to display the featured post:

    <?php
    $args = array(
    'posts_per_page' => 5,
    'meta_key' => 'meta-checkbox',
    'meta_value' => 'yes'
    );
    $featured = new WP_Query($args);

    if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post(); ?>
    <h3><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h3>
    <p class="details">By <a href="<?php the_author_posts() ?>"><?php the_author(); ?> </a> / On <?php echo get_the_date('F j, Y'); ?> / In <?php the_category(', '); ?></p>
    <?php if (has_post_thumbnail()) : ?>
    <figure> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> </figure>
    <p ><?php the_excerpt();?></p>
    <?php
    endif;
    endwhile; else:
    endif;
    ?>

Image if you want.

1

I think you need in your query is post__not_in but you aren't showing any code:

taken from WP_Query:

$query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );

if you want to exclude that particular post you're in you would do:

'post__not_in' => array($post->ID),

Other questions on the topic:

**

EDIT

**

Based on your code example I would take a guess you have two different querys going on. One for the full width image and the remaining for the image left and content right areas. I would still apply the same rule of 'post__not_in' => array($post->ID) but if you're asking to omit the feature category you would use 'category_not_in' => 'feature',. So the arguments would look like:

$args = array(
    'posts_per_page'    => 5,
    'meta_key'          => 'meta-checkbox',
    'meta_value'        => 'yes',
    'post__not_in'      => array($post->ID),
);
$featured = new WP_Query($args);

or

$args = array(
    'posts_per_page'    => 5,
    'meta_key'          => 'meta-checkbox',
    'meta_value'        => 'yes',
    'category_not_in'   => 'feature',
);
$featured = new WP_Query($args);

A few pointers I follow when coding:

  1. I do not see a wp_reset_postdata(); at the end of your loop. I've always been taught it's good to reset the query after it's been called. Reference "wp_reset_postdata() or wp_reset_query() after a custom loop?"

use the "what if" mentality:

  1. You call for the_category(', '); but you do not consider if you do not have a category checked so you should possibly consider has_category()
  • Thanks for help, I have used all the code in this article, can you help? smallenvelop.com/how-to-create-featured-posts-in-wordpress – Vy Spirit May 23 '17 at 14:30
  • Not clicking on a link to a site I am not familiar with. If you want to make an edit to your question to include the necessary code of the area you're working on others might be able to help but based on what you've posted that is the extent of what I'm able to help with. SE isn't a place to request free work or debugging. The amount of effort you put in a question will increase your chances of getting a better answer. – DᴀʀᴛʜVᴀᴅᴇʀ May 23 '17 at 14:33
  • Thanks for your comments. I have updated the code that I use in the article. – Vy Spirit May 23 '17 at 14:51
  • Thanks for help, the code I use also has two additional code placed in the function file to add a checkbox in the article. This checkbox is supposed to display the featured post via the code above. And after I try to revise the code like you, it still does not work :( – Vy Spirit May 23 '17 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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