1

I have a PHP template I am applying to several pages on my site so that they show a list of all posts in a category with the same name as the page. However, this is not working correctly in that posts from one category ('Events') do not show up at all on its page, while only one post from another category ('Results') is showing up on its page despite there being several posts in each.

I have checked that the category and page names are the same (end even that the posts only have that one category), so that is not the issue. Yet reading through the code, I can see no obvious errors.

I am working with the following code in my template file:

<?php /*
Template Name: List-all-posts-in-category
*/ ?>

<?php get_header(); ?>
<div id="main">
    <?php get_sidebar(); ?>
    <div id="content">
        <div id="content-post"> 
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                <div id="content-post-title">
                    <h1><?php the_title(); ?></h1>
                </div>
                <?php the_content(); ?>
            <?php endwhile; else: endif; ?>

            <?php $the_query = new WP_Query( array( 'category_name' => sanitize_title( get_the_title() ) )  ); ?>
            <?php $yearNumber = 'YEARNOTSET'; ?>

            <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <?php if ($yearNumber != get_the_date($d = 'Y')): ?>
                    <?php $yearNumber = get_the_date($d = 'Y'); ?>
                    <div id="content-category-post-date">
                        <h1><?php the_date($d = 'Y'); ?></h1>
                    </div>
                <?php endif; ?>
                <div id="content-category-post-title">
                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                </div>
            <?php endwhile; else: endif; ?>
        </div>
    </div>
</div>
<div id="delimiter">
</div>
<?php get_footer(); ?>

Any ideas what could be causing the issue?

Thanks in advance.

4
  • 1
    1. never use query_posts, this subject is covered extensively on this site. 2. you're using the_date, which echoes a value by default, in comparison and assignment, which will not work.
    – Milo
    Commented May 12, 2016 at 15:13
  • @Milo Thanks for that, I've modified the code to use WP_Query and get_the_date() instead, but I'm still having the same problem with posts not showing up in the page.
    – DTR
    Commented May 12, 2016 at 15:28
  • Have you doublechecked that the posts are really published? Commented May 12, 2016 at 15:41
  • @TobiasBeuving Yes, the posts are both public and published.
    – DTR
    Commented May 12, 2016 at 15:44

1 Answer 1

2

First off, ensure that the category is exactly the same name as the page ( slug and all ). Secondly, the parameters you're passing to WP_Query is incorrect.

  • category_name expects a slug. For example, Page 1 would have a slug of page-1.
  • get_the_title() pulls the current page title - so you're passing Page 1 to a parameter that expects a slug page-1.

What you can do is sanitize_title( get_the_title() ) or preferably you could declare global $post at the top of your document and pass instead $post->post_name.

4
  • Thanks. I've modified the query to use the sanitized title instead (updated code in question) and double-checked that the category and page names/slugs are all identical, yet one of the pages ('Events') is still showing no results.
    – DTR
    Commented May 12, 2016 at 15:58
  • @DTR and if you set 'post_type' => 'post', 'posts_per_page' => 10 still nothing? And this is a category we're talking about, not a tag?
    – Howdy_McGee
    Commented May 12, 2016 at 15:59
  • Correct, with or without those being added to the query, the page 'events' (now in lowercase so that everything is 100% the same) simply gives "Sorry, no posts matched your criteria."
    – DTR
    Commented May 12, 2016 at 16:12
  • Update: It seems that one of the plugins I had installed had a problem with me calling a page "Events", and was causing issues. Problem solved!
    – DTR
    Commented May 12, 2016 at 17:31

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.