Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
The second if ( have_posts() ) : while ( have_posts() ) : the_post(); is useless. Remove it and report back. –  toscho Mar 26 '13 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 Mar 26 '13 at 20:20
    
Maybe update your code above? –  vancoder Mar 26 '13 at 21:15
    
Check out my updates above - –  William Mar 27 '13 at 14:51
    
Whhere is the POST_TYPE constant defined? Also: remove the Template Name: header from your archive-event.php template file. –  Chip Bennett Mar 27 '13 at 15:18
show 1 more comment

2 Answers 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}.

share|improve this answer
    
I tried this and for some reason it isn't working - it just says 'No Results Found' –  William Mar 26 '13 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? –  Chip Bennett Mar 26 '13 at 22:09
    
Please check out my updates - I've included my register_post_type and new code. –  William Mar 27 '13 at 14:50
add comment

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.

share|improve this answer
    
That said, I agree with Chip. –  vancoder Mar 26 '13 at 18:25
    
I saw the $query->is_admin here (wpmu.org/query_posts) - should it be a method? –  William Mar 26 '13 at 21:04
    
Retracted - that part is correct. –  vancoder Mar 26 '13 at 22:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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