0

I am creating a custom Wordpress theme and as far as I can remember Wordpress is supposed to do the heavy lifting for filtering of post types by their category, author, etc. on it's own. This is just an altered query from my index.php specifically for category types.

Another question... do I even need an archive.php? I thought Wordpress was supposed to do all the heavy lifting as far as filtering by categories, tags, authors, etc. My index.php works great, but when I tried to go to a specific category it was showing all of my posts and not just the selected category so I tried implementing an archive.php

EDIT 1: I should have mentioned I'm not using the main loops because I'm using custom post types

archive.php

<?php
/**
 * @author    Mark Abel
 * @package   client-name/archive
 * @version   1.0
 */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
get_header(); ?>

<?php get_template_part( 'partials/partial', 'sidenav' );?>

<section class="masonry__container" id="masonry__container">
  <div class="masonry" id="masonry">
    <?php
      $category = get_category( get_query_var( 'cat' ) );
      $cat_id = $category->cat_ID;
      $loop = new WP_Query( array( 'post_type' => 'project', 'cat' => $cat_id ) );
      if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <?php $project_for = get_post_meta($post->ID, "project_for", true); ?>
          <article class="masonry__tile" data-aos="zoom-in">
            <img src=<?php the_post_thumbnail_url(); ?> alt="an image" />
              <div class="masonry__overlay"><div class="masonry__title">
                <?php echo get_the_title() ?>
              </div>
              <div class="masonry__project">
                <?php echo $project_for ?>
              </div>
              <span></span>
              <div class="masonry__cta"><a href="<?php echo get_post_permalink(); ?>">Click to view</a></div>
              <a class="masonry__video" href="https://player.vimeo.com/video/124254859">Play video</a>
            </div>
          </article>
        <?php endwhile;
      endif;
      wp_reset_postdata();
    ?>
  </div>
</section>


</body>

<!-- Footer --> 
<?php get_footer(); ?>

</html>

partial imported by index.php

<?php
/**
 *  @author    Mark Abel
 *  @package   client-name/partials/partial-masonry
 *  @version   1.0
 */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>

<section class="masonry__container" id="masonry__container">
  <div class="masonry" id="masonry">
    <?php
      $loop = new WP_Query( array( 'post_type' => 'project' ) );
      if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <?php $project_for = get_post_meta($post->ID, "project_for", true); ?>
          <article class="masonry__tile" data-aos="zoom-in">
            <img src=<?php the_post_thumbnail_url(); ?> alt="an image" />
              <div class="masonry__overlay"><div class="masonry__title">
                <?php echo get_the_title() ?>
              </div>
              <div class="masonry__project">
                <?php echo $project_for ?>
              </div>
              <span></span>
              <div class="masonry__cta"><a href="<?php echo get_post_permalink(); ?>">Click to view</a></div>
              <a class="masonry__video" href="https://player.vimeo.com/video/124254859">Play video</a>
            </div>
          </article>
        <?php endwhile;
      endif;
      wp_reset_postdata();
    ?>
  </div>
</section>
1
  • Just a wild guess.. Have you set the has_archive argument to true while registering the CPT? It's false by default.
    – Abhik
    Sep 23, 2019 at 3:13

1 Answer 1

0

I thought Wordpress was supposed to do all the heavy lifting as far as filtering by categories, tags, authors, etc.

It does. Which is why you shouldn't have done this:

  $category = get_category( get_query_var( 'cat' ) );
  $cat_id = $category->cat_ID;
  $loop = new WP_Query( array( 'cat' => $cat_id ) );

In the main template files you shouldn't be doing your own queries. Use the main loop:

<?php if ( have_posts() ) : ?>
      <?php while ( have_posts() ) : the_post(); ?>
            <!-- etc. -->
      <?php endwhile; ?>
<?Php endif; ?>

The purpose if archive.php is to allow you to have a different template for archives. Not to manually query different posts. The only difference should be the HTML markup around the content.

9
  • Like I said... I didn't try implementing archive.php until after index.php wasn't filtering correctly.
    – Mark
    Sep 23, 2019 at 0:55
  • I posted my index.php below if you can notice anything wrong. I'm using custom post types.
    – Mark
    Sep 23, 2019 at 0:58
  • You’ve made the same mistake with index.php Sep 23, 2019 at 1:42
  • 1
    That's a different question altogether. You need to do what I've suggested and fix your templates to only use the main loop. That fixes your theme. index.php is not the homepage template. It's used for all lists of posts. Then you need to post a question asking how to display a custom post type on the homepage, or do a search, because it's definitely been asked before. Sep 23, 2019 at 4:24

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.