1

I am pretty new in WordPress development and I am trying to implement this custom theme that handle the so called featured posts

As you can see in the posts area of the homepage I have the Articoli in evidenza sub area that contains my featured posts and under it the Ultimi Articoli subarea that contains the latest posts.

To implment this I use the posts tag and in the futured posts area I show the posts having the tag=featured condition.

So this is my code:

<section id="blog-posts">

<header class="header-sezione">
        <h2>Articoli in evidenza</h2>
</header>

<?php query_posts('tag=featured');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <div id="featured-posts">

    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
      <div class="meta">
Scritto da <span class="author"><?php the_author_link(); ?></span> &nbsp;//&nbsp;  <?php the_category(', ') ?>  &nbsp;//&nbsp;  <?php comments_popup_link('Nessun Commento', '1 Commento ', '% Commenti'); ?> 
      </div>
      <div class="featured-details"><?php the_excerpt()?>
      <?php $featured_img = get_post_meta($post->ID, 'featured_img', $single = true); ?>
      <a href="<?php the_permalink(); ?>"><img src="<?php echo $featured_img ?>" alt="<?php the_title(); ?>" /></a>
      </div>
    </div>

<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>



    <header class="header-sezione">
        <h2>Ultimi Articoli</h2>
    </header>

    <?php
    if (have_posts()) :
        // Start the Loop.
        while (have_posts()) : the_post();

            /*
             * Include the post format-specific template for the content. If you want to
             * use this in a child theme, then include a file called called content-___.php
             * (where ___ is the post format) and that will be used instead.
             */
            get_template_part('content', get_post_format());

        endwhile;
    else :
        // If no content, include the "No posts found" template.
        get_template_part('content', 'none');

    endif;
    ?>

</section>

As you can see first I show the posts having a tag featured by the use of query-posts() function:

<?php query_posts('tag=featured');?>

Now my problem is that, if a post have the featured tag, I don't want that it is shown in the latest post area (at this time it is shown). So I tried to use this code:

<header class="header-sezione">
    <h2>Ultimi Articoli NOT FEATURED</h2>
</header>

<?php query_posts('tag != featured');?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <div id="featured-posts">

    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
      <div class="meta">
Scritto da <span class="author"><?php the_author_link(); ?></span> &nbsp;//&nbsp;  <?php the_category(', ') ?>  &nbsp;//&nbsp;  <?php comments_popup_link('Nessun Commento', '1 Commento ', '% Commenti'); ?> 
      </div>
      <div class="featured-details"><?php the_excerpt()?>
      <?php $featured_img = get_post_meta($post->ID, 'featured_img', $single = true); ?>
      <a href="<?php the_permalink(); ?>"><img src="<?php echo $featured_img ?>" alt="<?php the_title(); ?>" /></a>
      </div>
    </div>

<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

But this don't work and the featured posts still shown in the homepage. As you can see, I've tried to specify that, to be shown, a post can't have the featured tag:

<?php query_posts('tag != featured');?>

Why don't this work? What am I missing?

3 Answers 3

3

There are a few errors which I want to address

First of all, you should never use query_posts to construct custom queries. This is not just my emphasis, but the codex as well. The one big problem with query_posts is, it many circumstances, pagination fails

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

Secondly, never run unnecessary queries where it is not needed. The main query can very easily be modified with the use of pre_get_posts, which saves on unnecessary database queries being performed by the use of custom queries

This hook is called after the query variable object is created, but before the actual query is run.

Thirdly, if you don't have a choice, and you have to run custom queries, do it with WP_Query or get_posts instead of query_posts

Also, you are abusing php tags. It is not necessary to open and close php tags after each piece of code, unless you are switching between php and html. For instance

 </div>
    </div>

<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

</div>

can be rewritten as

</div>
    </div>

<?php 
   endwhile;
   else :
   endif;
?>

</div>

This is how I will tackle the problem. I do not say it is the best solution, but it is a better and cleaner method to achieve your goal

Your first query for your featured content should look something like this

<section id="blog-posts">

<header class="header-sezione">
        <h2>Articoli in evidenza</h2>
</header>

<?php 

$featured = new WP_Query('tag=featured');

if ($featured->have_posts()) : 
  while ($featured->have_posts()) : $featured->the_post(); ?>

   <---YOUR LOOP ELEMENTS--->

<?php 
  endwhile; 

  wp_reset_postdata();

endif; 
?>

Your main loop is fine as is, I will not change anything to that. DO NOT use a custom query here to exclude a tag. Use pre_get_posts to do that. Here is how

In your functions.php, add the following code to remove posts from the featured tag from the main query on your home page. You will be using the is_home() conditional tag to target the home page

EDIT

I forgot to include this. Is is always fail safe to include a check to see if you are not on a admin page (!is_admin()). The reason being, pre_get_posts alters the main query which is used both on front end and backend, thus all changes will be seen front end and back end. You just want to make changes on front end, so the modified code will be

function exclude_featured_tag( $query ) {
    if ( !is_admin() && $query->is_home() && $query->is_main_query() ) {
        $query->set( 'tag__not_in', array('ID OF THE FEATURED TAG') );
    }
}
add_action( 'pre_get_posts', 'exclude_featured_tag' );
3
  • Tnx, perferct explaination :-) Commented Jul 20, 2014 at 17:24
  • My pleasure, glad I could shed some light on your problem. Enjoy :-) Commented Jul 20, 2014 at 17:25
  • Please see my edit. It is very important. Thanks and sorry for any confusion Commented Jul 28, 2014 at 10:26
1
<?php
$args = array(
'posts_per_page' => 5,
'cat' => 2,
'tag__not_in' => array(5), // Assuming 5 is the ID for the tag
'order' => 'asc'
 );

 $query = new WP_Query($args);
 while ($query->have_posts()) :
 $query->the_post();

 ...

  endwhile;
   // don't forget to reset/restore the query
   wp_reset_postdata();

 ?>

make sure that you must give tag id number to tag_not_in. dont give tag name to it.it won't work with tag name.

-1

Solved by myselfe using:

<?
    // get the term using the slug and the tag taxonomy
    $term = get_term_by( 'slug', 'featured', 'post_tag' );
    // pass the term_id to tag__not_in
    query_posts( array( 'tag__not_in' => array ( $term->term_id )));
?>
1
  • 2
    Don't use query_posts ever. You should be using WP_Query for your custom query and pre_get_posts to alter your main query Commented Jul 17, 2014 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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