1

I have a custom post type in WordPress with custom meta boxes.

So I need a hierarchal post and also have a custom template set for each of the post (either Type1 or Type2).

This is how my slug looks,

http://example.com/taxonomy/type1 - for the primary post

http://example.com/taxonomy/type1/type2 - for the secondary post

The problem is that the type 2 page is essentially the same as the type1 just with a bit of additional information. And my client plans to have many posts in type1 which will lead to more posts in type2 and this will make managing hard.

I can save all the content I need for type2 posts within the parent type1 using meta boxes. I just need a way to point the URLs to the right data.

So if I access this URL, http://example.com/taxonomy/type1/type2, it will open primary post and load the data for it from a meta box (also has to load another template file). I want this done via php and don't want to load all the content and edit it frontend using javascript.

Update:

Sorry if I was a bit confusing. I already have a custom post type with a metabox that allows me to select a template (and also show additional metaboxes depending on the template).

My slug already has the custom taxonomy I am using (used %name% as the slug in the argument for creating posts.

And my posts slug work fine as long as I choose the right parent post. My question is, instead of creating a custom post (type2) as a child to the type1 post, how can I make wordpress redirect http://example.com/taxonomy/type1/type2 to the type1 post and also get the text of type2 within a php function where I can print out the template for the pages.

Edit

add_filter('query_vars', 'add_type2_var', 0, 1); 

function add_type2_var($vars){ $vars[] = 'type2'; return $vars; }

add_rewrite_rule('/?apk/(.[^/])/(.[^/])/(.*)$',"/wp/apk/$1/$2?type2=$3",'top');

I have added this code to my theme and did not modify .htaccess. The type2 var shows up correctly when I use a child post url or if I add numbers at the end. I set hierarchical to false for the custom post type and the link still shows 404. Is there a way for me to bypass the 404? My guess is that I am trying to get the url using the single_template filter. But I should validate it somewhere before wordpress gives a 404.

2 Answers 2

0

Based on the new details in the question:

Instead of creating a separate "type2" post type, you could:

  • Put all the data together in the "type1" post
  • Have the "type2" permalink (example.com/taxonomy/type1/type2/) redirect to: example.com/taxonomy/type1/?type=type2 (you can make this query string whatever you want, but the gist is, redirect to a query string version of the "type1" permalink). You should be able to code this to a post status hook - whenever a post status changes to 'publish', if the post type is 'type1', add a wp_redirect from the URL structure you want people to type in to the query string version.
  • Make the "type1" template conditional. Use an if statement: if there is a $_GET request where type=type2, show the type2 content; else, show the type1 content.
6
  • Since there are many steps to this process I'd suggest researching each one and as you run into roadblocks post a more specific question. Try a few things and post your code and you will tend to receive more responses. :) Commented Apr 18, 2017 at 14:35
  • Hey, I have edited the question and added details. I already know how to have two types of post and display the template for each of them. Here is an example, gyazo.com/cc9feaf8319f6e567d7c58bb161e19b5 (where dd is the type1 post also set as parent, and default is the taxonomy). type2 is not the same content rather a more specific content of the type1. (for example i can have a download page of an app as my type1 post and my type2 post (which adds a /download to the end of the url) would show a text saying that the download is gonna start in a few seconds. Commented Apr 20, 2017 at 14:09
  • Though thats just an example, I wish to pull some custom data from an array depending on the type2 link. I only need to know how to get the type2 text in a php function and I could manage from there on my own. Commented Apr 20, 2017 at 14:13
  • Updated suggestions above. Commented Apr 20, 2017 at 14:55
  • Thanks for the update. Is there any way to redirect example.com/taxonomy/type1/type2/ to example.com/taxonomy/type1/?type=type2 within WP instead of using .htaccess? And I dont understand where I need to add wp_redirect (from type1 post to type2?) Commented Apr 21, 2017 at 14:36
0

Sounds like what you need is a separate endpoint. If you have all of the information in the type1 post, then you just want to point to a different template if there is a folder extension.

So starting with your main url:

http://example.com/post-name/ 

and adding a path as an endpoint (ex: moreinfo):

http://example.com/post-name/moreinfo/ 

To do this, you'd use the add_rewrite_endpoint() function.

add_rewrite_endpoint( 'moreinfo', EP_PERMALINK ); 

This essentially does what WebElaine was suggesting without having to redirect, so you could add to your template:

if( get_query_var( 'moreinfo', false ) !== false ) { // Display additional information } 

Hope this helps!

1
  • the endpoint link is different for each type1 post and there can be many type2 posts for one type1 post. Commented Apr 21, 2017 at 14:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.