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, …
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?
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:
Then we define in functions.php this new rewrite tag:
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: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: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:
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.