0
votes

I am completely not sure if I am just being blind but not 100% used to wordpress as yet and would love some assistance with this.

I have my blog posts page showing all the blog posts it also has the categories in the sidebar but when a category is selected it changes the URI with the /categories/events but does not limit the posts content to what should be in the category?

<?php get_header(); ?>

<div class="container posts">
  <div class="row">
    <div class="col-xs-8 col-md-8">
      <?php
        $args = array (
          'post-type' => 'post'
        );

        $post_query = new WP_Query($args);
        if ($post_query->have_posts())
          while ($post_query->have_posts()) {
            $post_query->the_post();
      ?>
      <div class="post">
        <?php
          the_post_thumbnail( 'large' );  
          echo '<a href="' . get_permalink() . '">';
          the_title('<h2>', '</h2>');
          echo '</a>';
          the_excerpt();
          echo do_shortcode( "[icon name='fa-calendar-o']" ) . " ";
          the_date("d F");
        ?>
      </div>
      <?php
          }
      ?>
    </div>
    <div class="col-xs-4 col-md-4">
      <?php get_sidebar(); ?>
    </div>
  </div>
</div>

Any ideas? This is a full custom WP Theme from start to finish so I may be missing something very simple and if someone knows what that is I would greatly appreciate it.

1
And where is the filter code? - Bilal Hussain
Well, that code creates a brand new WP_Query object that retrieves all posts. This is not the typical way of doing things, which would be simply to use the existing WP_Query object that's been set up by WordPress and would have the right parameters to retrieve a Category's posts on a Category page... What's the filename of the template page you've posted? - Matt Gibson
@MattGibson The file name is index.php the home page is a seperate file name. I have never needed to do categories before so no clue on how to get something like this to work... - Paul Murdoch
@PaulMurdoch WordPress would generally handle this for you automatically. Instead of creating a new WP_Query object, you should be able to just get rid of your $post_query object completely and use raw have_posts()/the_post() calls as explained in the The Loop documentation. WordPress will use an existing query object that's already been set up appropriately for the kind of page you're displaying, based on its URL/parameters. - Matt Gibson
@MattGibson That worked! It is pulling through the categories perfectly now! Thank you man - If you post as Answer I will select +rep :) - Paul Murdoch

1 Answers

0
votes

Before loading your index.php page, WordPress will have set up a WP_Query object for you according to the page URL/parameters. So on the main blog posts page, it will be a query that finds all posts; on the Foo category page it'll be a query that finds all posts in category Foo.

In your existing code, you're ignoring that pre-set-up query object and creating a new one, $post_query, that always just grabs all posts. That's why the results aren't changing when viewing a Category.

You should dump your specially-created $post_query object and use the basic WordPress "The Loop" code instead:

    if (have_posts())
      while (have_posts()) {
        the_post();
        ...

This will use the WP_Query object that's been set up for you by WordPress. Typically you don't create your own WP_Query object unless you're doing something a little out of the ordinary.