How to set up my WordPress template structure for handling my custom post types

I’m having trouble setting up my WordPress template. I’m not sure how to achieve the following. My website setup is the following:

I have a Custom Post Type “Chapter”. A “Chapter” is parent to other CPT’s. I have a few post types such as: reviews, interviews, blogposts, …

Read More

On a chapter page I do a different WP_Query for every post type within this chapter.

page template single-chapter.php

Now I want to be able to click on “Blogpost (2)” and open an archive page of all blogpost within the current chapter. How can I achieve this? I assume I should create a template page “archive-blogpost.php”. I already found that I can link to this page using:

<?php echo get_post_type_archive_link( 'blogpost' ); ?>

However I can’t see how this page could know in what chapter i’m currently in?

Related posts

2 comments

  1. I never used the WP Types plugin so far, but here is what you could do: add a chapter parameter to the archive link (with a rewrite rule for it), that you’ll get in your blogposts archive template and display the posts conditionally to this parameter.

    First we change the archive link to send the chapter slug:

    <?php echo get_post_type_archive_link( 'blogpost' ) . '/' . $post->post_name; ?>
    

    Then we define in functions.php this new rewrite tag:

    function custom_rewrite_tag() {
        add_rewrite_tag('%chapter%', '([^&]+)');
    }
    add_action('init', 'custom_rewrite_tag', 10, 0);
    

    To make the URL looks good, we’ll add a rewrite rule (so you don’t have a url like /archive/?chapter=chapter-1, but /archive/chapter-1) – this still go to functions.php:

    function custom_rewrite_rule($rules) {
        add_rewrite_rule('^archive/([^/]+)/?$', 'index.php?post_type=blogposts&chapter=$matches[1]', 'top');
    }
    add_filter('init', 'custom_rewrite_rule', 10, 0);
    

    You may have to change the URL / post type name depending of your configuration.

    And last, you can get this query arg in your blogposts archive template with $wp_query->query_vars array:

    $wp_query->query_vars['chapter']
    

    Since I don’t know much about WP Types, I’m not really sure about what follows, but it seems you could query the childs posts of the chapter with this:

    if(isset($wp_query->query_vars['chapter']) && $chapter = get_page_by_path($wp_query->query_vars['chapter'])) {
        $childargs = array(
            'post_type' => 'blogposts',
            'numberposts' => -1,
            'meta_query' => array(array('
                key' => '_wpcf_belongs_property_id', 'value' => $chapter->ID
            ))
        );
        $child_posts = get_posts($childargs);
    } else {
        // default template : display all posts
    }
    
  2. I used a WP types functions to achieve what I wanted:

    $child_posts = types_child_posts(“blogpost”, array(‘post_id’ => $_GET[‘wpv-pr-child-of’]));

    With this I can query all the child posts from the custom post type on a page.

    I also use a session variable to keep track of the current chapter I’m in.

Comments are closed.