0

I am trying to create a template that will display a list posts of a specific custom type. However, I am missing something fundamental. So far, here is what I have:

functions.php

add_action( 'pre_get_posts', 'query_filter', 1);
function query_filter($query)
{
    if (!$query->is_admin            OR
         $query->is_main_query()     OR
         is_page_template( 'page-events.php' ))
    {
        $query->query_vars['posts_per_page'] = 3;
    }

    return $query;
}

page-events.php

<?php
/*
Template Name: Events Page
*/
?>

<?php get_header(); ?>

<?php get_template_part('includes/breadcrumbs'); ?>
<?php get_template_part('includes/top_info'); ?>

<div id="content" class="clearfix fullwidth">
    <div id="left-area">
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <div>
                <?php the_content(); ?>
                    <div>
                        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                            <h2>
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_title(); ?>
                                </a>
                            </h2>
                        <?php endwhile; endif; ?>
                    </div>
            </div>
        <?php endwhile; endif; ?>
    </div>
</div>

<?php get_footer(); ?>

As you can see, I still haven't attempted to filter for the custom post type, because currently what is happening is the 'the_content()' is being repeated over and over 100's of times (there should only be about 12 posts). In addition, the_title() doesn't appear anywhere on the page.

What am I missing?

  • William

UPDATE

After a few suggestions, here is my updated code:

EventManager.php

This is where my custom post type is created.

add_action('init', 'Initialize');
static function Initialize()
{
register_post_type(Event::POST_TYPE,
    array(
        'labels' => array(
            'name'                => 'Events',
            'singular_name'       => 'Event',
            'add_new'             => 'Add New',
            'add_new_item'        => 'Add New Event',
            'edit_item'           => 'Edit Event',
            'new_item'            => 'New Event',
            'view_item'           => 'View Event',
            'search_items'        => 'Search Event',
            'not_found'           => 'No Events Found',
            'not_found_in_trash'  => 'No events found in trash',
            'menu_name'           => 'Events',
            ),
        'public'               => true,
        'publicly_queryable'   => true,
        'query_var'            => true,
        'rewrite'              => false,
        'show_ui'              => true,
        'hierarchical'         => false,
        'has_archive'          => true,
        'menu_icon'            => JUPITER_PLUGIN . 'images/small-greyscale.png',
        'supports' => array(
            'title',
            'editor',
            'author',
            'thumbnail',
            'excerpt',
            'comments',
            'revisions'
            ),
        )
);

global $wp_rewrite;

$gallery_structure = '/' . Event::SLUG . '/%year%/id=%event_id%/%event%';
$wp_rewrite->add_rewrite_tag("%event%", '([^/]+)', "event=");
$wp_rewrite->add_rewrite_tag("%event_id%", '([0-9]+)', "event_id=");
$wp_rewrite->add_permastruct('event', $gallery_structure, false);

add_filter('post_type_link', 'gallery_permalink', 10, 3);
function gallery_permalink($permalink, $post_id, $leavename)
{
    $post = get_post($post_id);

    if ($post->post_type == Event::POST_TYPE)
    {
        $rewritecode = array(
            '%year%',
            '%event_id%'
            );

            $unixtime = strtotime($post->post_date);

            $date = explode(" ",date('Y m d H i s', $unixtime));

            $rewritereplace = array(
                $date[0],
                $post->ID
                );

            $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
            return $permalink;
    }
    return $permalink;
}

archive-event.php

<?php
/*
 * Template Name: Events Archive
*/
?>
<?php get_header(); ?>

<?php get_template_part('includes/breadcrumbs'); ?>
<?php get_template_part('includes/top_info'); ?>

<div>
    <div>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <div>
            <?php echo the_title(); ?>
        </div>
    <?php endwhile; endif; ?>
    </div>
</div>
<?php get_footer(); ?>

This should be the page that is automatically loaded when I navigate to domain.com/event. However, that address currently says 'No Results Found'. If I don't include the custom rewrites when creating the custom post type, then this works as expected. Do I have to include some sort of additional rewrite to redirect the archive to this page?

6
  • The second if ( have_posts() ) : while ( have_posts() ) : the_post(); is useless. Remove it and report back.
    – fuxia
    Commented Mar 26, 2013 at 18:02
  • I just did - I removed everything between (and including) the redundant inner loop. I had thought that this wasn't right but I found it as an example somewhere else. Now all I see is the stuff generated by the_content(). When I try to add in an echo the_title() it returns the title of the page but no information about posts.
    – William
    Commented Mar 26, 2013 at 20:20
  • Maybe update your code above?
    – vancoder
    Commented Mar 26, 2013 at 21:15
  • Check out my updates above -
    – William
    Commented Mar 27, 2013 at 14:51
  • Whhere is the POST_TYPE constant defined? Also: remove the Template Name: header from your archive-event.php template file. Commented Mar 27, 2013 at 15:18

2 Answers 2

2

Why reinvent the wheel? Just create archive-{posttype}.php, and ensure that 'has_archive' is set to true for the post type.

If your post type is event: archive-event.php.

Then, WordPress will handle the query automatically. If pretty permalinks are enabled:

example.com/{posttype}

...will display the archive index page for {posttype}.

3
  • I tried this and for some reason it isn't working - it just says 'No Results Found'
    – William
    Commented Mar 26, 2013 at 21:00
  • Please post actual code in your question, otherwise it's impossible to troubleshoot or help. In this case: what is your register_post_type() call? Commented Mar 26, 2013 at 22:09
  • Please check out my updates - I've included my register_post_type and new code.
    – William
    Commented Mar 27, 2013 at 14:50
1

You've duplicated your loop:

   <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <div>
            <?php the_content(); ?>
                <div>
                    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Hence the hundreds or repetitions.

3
  • That said, I agree with Chip.
    – vancoder
    Commented Mar 26, 2013 at 18:25
  • I saw the $query->is_admin here (wpmu.org/query_posts) - should it be a method?
    – William
    Commented Mar 26, 2013 at 21:04
  • Retracted - that part is correct.
    – vancoder
    Commented Mar 26, 2013 at 22:28

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.