0

I have a custom post type by the name of the event.

So Created a category page them for them like this →

category-event.php

Like this →

<?php get_header(); ?>
//and then the loop here
<?php get_footer(); ?>

But the individual category pages are not generating the posts only from those categories, but entirely all posts.

I think something needs to be fixed in the loop?

Update → the Loop

$the_query = new WP_Query( array(
        'post_type' => 'event',
        'posts_per_page' => 10,
        'post_status' => 'publish',
        )  );
    ?>
<?php if ( $the_query->have_posts() ) : ?>
    <!-- the loop -->
    <div class="class1">
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <div class="class2">
            <div class="class3">
                <?php $url = esc_url( get_post_meta( get_the_ID(), 'video_oembed', true ) ); ?>
                <?php $embed = wp_oembed_get( $url ); ?>
                <div class="class4">
                    <iframe id="class_frame" width="560" height="315" src="https://www.youtube.com/watch"); ?>" allowfullscreen frameborder="0"></iframe>
                </div>
            </div>
            <div class="class6">
                <h1><?php the_title(); ?></h1>
                <p><?php the_content(); ?></p>
            </div>
        </div>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    </div>
    <!-- end of the loop -->
    <!-- <?php wp_reset_postdata(); ?> -->
<?php endif; ?>
  • well, post the loop then ;) the way i see it, you don't use the main query, but create a new one. and that will not contain the main queries argument, unless you also put 'em in there, too. – honk31 Jun 12 '18 at 10:30
1

You may alter the main query before the loop, but it's crucial to reset the main query afterwards. Otherwise you'll probably run into problems elsewhere.

Disclaimer: Doing this is officially discouraged by WordPress here. However, in my experience it works perfectly to achieve the behaviour you want. Just don't forget to add wp_reset_query(); after closing the loop.

<?php query_posts('post_type=event'); ?>
  <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>

    <!-- Stuff happening inside the loop -->

  <?php endwhile; endif; ?>
<?php wp_reset_query(); ?>

Another approach (slightly more complicated but without altering the main query) would be:

<?php $args = array( 'posts_per_page' => 10, 'post_type' => 'event', 'post_status' => 'publish' ); ?>
<?php $get_category_posts = get_posts( $args ); ?>
<?php foreach ( $get_category_posts as $post ) : setup_postdata( $post ); ?>

  <!-- Stuff happening inside our custom 'loop' -->

<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

According to the 2nd solution, your PHP-File should look like this:

<?php get_header(); ?>

<?php $args = array( 'posts_per_page' => 10, 'post_type' => 'event', 'post_status' => 'publish' ); ?>
<?php $get_category_posts = get_posts( $args ); ?>
<?php foreach ( $get_category_posts as $post ) : setup_postdata( $post ); ?>

  <div class="class1">
    <div class="class2">
      <div class="class3">

        <?php print_r( get_post_meta( get_the_ID() ) ); // Just for demo purposes ?>

        <?php // $url = esc_url( get_post_meta( get_the_ID(), 'video_oembed', true ) ); ?>
        <?php // $embed = wp_oembed_get( $url ); ?>

        <div class="class4">
          <iframe id="class_frame" width="560" height="315" src="https://www.youtube.com/watch" allowfullscreen frameborder="0"></iframe>
        </div>
      </div>
      <div class="class6">
        <h1><?php the_title(); ?></h1>
        <p><?php the_content(); ?></p>
      </div>
    </div>
  </div>

<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

<?php get_footer(); ?>

If this doesn't work it might be that you're working in the wrong template file. Without knowing your project my guess would be that it should be archive-event.php

Update:

It turned out to be taxonomy-event.php in this case.

  • Doing this is officially discouraged by WordPress → Whats the alternative solution? – The WP Novice Jun 12 '18 at 10:35
  • 1
    @TheWPNovice See the alternative approach I've just added to my original answer. – Nico Jun 12 '18 at 11:13
  • 1
    hi @Nico :) nope. i wouldn't go that route eather. actually you are calling the main query here, but then you never use it. i'd rather alter the main query (if ever there is a need for) via pre_get_posts.. – honk31 Jun 12 '18 at 11:19
  • 1
    @honk31 You're right. I removed the unnecessary loop. Thanks for the heads up. – Nico Jun 12 '18 at 12:20
  • @TheWPNovice I think you're either getting mixed up between different solutions or working inside the wrong template-file. Try copying the "your PHP-File should look like this"-Part I've added to my answer into your PHP-File. If it's the correct file it should work as expected. If your website is not showing the get_post_meta-Array afterwards, you're in the wrong file. – Nico Jun 13 '18 at 8:11
2

The problem of yours is, you run your own query, and make no use of the main $wp_query. try to use that one:

<?php
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        //whatever 
    endwhile;
endif;

and NOT

$the_query->have_posts()
  • category-event.php → the event category pages will take this template? – The WP Novice Jun 12 '18 at 10:52
  • 1
    if you have a category with the slug event, and your events are in there, then yes. install this plugin: Show Current Template and visit the page you are talking about. it will name all currently used template files in the admin-bar – honk31 Jun 12 '18 at 10:54
2

I think you need to make use of the template hierarchy. So, according to the information given of your custom post type, the name of the category template should be: category-event.php

Sure, you also need to have the right query in place (in your "category-event.php" file) in order to get the correct posts.

// Custom Query for CPT 'tires'
$custom_arguments = array(
    'post_type' => 'event',
    'posts_per_page' => '36',
    'taxonomy' => 'your-event-categories',
    'orderby'=> 'name',
    'order'=> 'ASC'
);
$loop = new WP_Query($custom_arguments);

$html = '';
// Build your HTML output
$html .= '<div class="event-archive">';
    while ($loop->have_posts() ) : $loop->the_post();
        $html .= Your HTML Markup to display the content
        $html .= '<div class="gridbox">';

        endwhile;
        ....
        wp_reset_query();

I hope this helps.

  • so this is the clue → ` wp_reset_query();` – The WP Novice Jun 12 '18 at 10:09
  • What do you mean by clue? As Nico also mentioned, it is important to reset the query variable to empty / reset the query. You should check the custom arguments in first place, if they are correct. – user3135691 Jun 12 '18 at 10:11
  • I have done it like this → screencast.com/t/b5wRjyhQC But the issue is it is still fetching all the posts not just of that category. – The WP Novice Jun 12 '18 at 10:26
  • None of the solutions are currently working. the individual category pages are pulling posts from all the categories not just one. – The WP Novice Jun 12 '18 at 18:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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