2

I'd like my homepage to only display posts from a single tag. Is this possible? If yes, please advise.

For instance,

www.mysite.com/tag/sometag

will only display posts with the sometag tag, but how do I get www.mysite.com to automatically display only the posts seen on

www.mysite.com/tag/sometag page?

0

3 Answers 3

5

You should use pre_get_posts to alter the main query on the home page.

With the proper conditional tags and parameters (check WP_Query for available parameters) you can achieve what you need

You can do the following to just display posts from a given tag on your homepage

add_action( 'pre_get_posts', function ( $query ) {
    if ( !is_admin() && $query->is_home() && $query->is_main_query() ) {
        $query->set( 'tag', 'SLUG_OF_TAG' );
    }
});
5
  • ps: I think we can simplify it further and skip !is_admin() here because of the is_home check.
    – birgire
    Jan 12, 2015 at 9:36
  • @birgire I agree. The use of !is_admin() has become such a habit for me with pre_get_posts, I tend to overuse it sometimes, specially with is_home() checks ;-) Jan 12, 2015 at 9:40
  • hehe, same with me, it's usually the first condition that I write down regardless of other conditions ;-) But sometimes I use is_admin() outside the callback function if I'm playing with stuff and tired of the usual if conditions ;-)
    – birgire
    Jan 12, 2015 at 9:45
  • Hahaha, absolutely exactly the same here. Jan 12, 2015 at 9:46
  • Does this work with WP version 4.9.7? In which file (and where inside that file) do I need to add the code above?
    – johann_ka
    Aug 1, 2018 at 12:26
0

For that; first you have to make Custom Page template and add following loop in it and customize it in what ever way you want.

<?php

    $args=array(
      'tag' => 'yourtag',
      'showposts'=>5     // set number of post you want to display
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>

        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

        // display what ever you want in this post

      <?php
      endwhile;
    }
  wp_reset_query();  // Restore global post data stomped by the_post().
?>

Then create a Page and select this custom page template for it.

Next from settings set this page as static home page and you are ready to GO.

3
  • No, don't use a custom query. Jan 9, 2015 at 18:14
  • 1
    @PieterGoosen thanks for introducing some great and straight forward stuff up there. Jan 10, 2015 at 17:34
  • My pleasure. For your reading, please feel free to check this post I have done a while back regarding where to use custom queries and where not and what are the pro's and con's Jan 10, 2015 at 17:38
-1

Custom wp queries will work perfectly, but in many cases pagination or a other elements gone or be "broken".

I use a not a ideal but "simplest" way: 301 redirection in Custom Page template for a orginal WP tag page.

Example piece of code:

<?php
/**
 * Template Name:  Your Custom Redirect Name
 */
header("HTTP/1.1 301 Moved Permanently");
header("Location: hxxp://yoursite.com/tag/your-tag-slug/");
 ?>

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.