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?

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' );
    }
});
  • ps: I think we can simplify it further and skip !is_admin() here because of the is_home check. – birgire Jan 12 '15 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 ;-) – Pieter Goosen Jan 12 '15 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 '15 at 9:45
  • Hahaha, absolutely exactly the same here. – Pieter Goosen Jan 12 '15 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 '18 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.

  • No, don't use a custom query. – Pieter Goosen Jan 9 '15 at 18:14
  • 1
    @PieterGoosen thanks for introducing some great and straight forward stuff up there. – Mohammad Mursaleen Jan 10 '15 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 – Pieter Goosen Jan 10 '15 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.