Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Post Types under regular pages? WordPress

Hello, I am trying to organize a custom post type under regular pages:

for instance I have a custom post type: fruit
and I have pages: north, south, east, west

now I want to be able to add "fruits" to the pages and have the permalink structure to be like normal pages, so that the fruits seem to be a part of the page structure like this:

mysite.com/north/apple ("apple" is a post of the "fruit" post type)

I have parented the fruit type posts to pages. But permalinks return 404, also they currently look like this: mysite.com/fruit/north/apple (which return 404)
only way to access "apple" is by typing "mysite.com/fruit/apple", even though parent is set to "north"

The reason for this is to be able to administrate "fruit" pages different from "normal" pages. But I would still like them to be permalinked as normal pages.

Any idea how I can solve this?

Answers (3)

2012-10-18

paul de wouters answers:

you may want to try the post2post plugin
http://wordpress.org/extend/plugins/posts-to-posts/


docz comments:

I really want to be able to solve this without plugins.

2012-10-18

Dbranes answers:

hi, you might try this code (put it into functions.php) to get url structure like:

example.com/north/apple

where apple is a post in the custom post type "fruit"

function my_rewrite_rules( $rules ) {
$newrules = array();
$newrules['^north/([^/]+)$'] = 'index.php?fruit=$matches[1]&post_type=fruit';
return $newrules + $rules;

}
add_filter('rewrite_rules_array','my_rewrite_rules');



and remember to save permalinks.




docz comments:

problem with that is that then I would have to hardcode a new rewrite rule for each page, I really need pages to be able to be added dynamically.

maybe it is possible to get

mysite.com/page/post_type/post_name? is that easier?


Dbranes comments:


you could try this rule instead:

$newrules['^([^/]+)/([^/]+)$'] = 'index.php?news=$matches[2]&post_type=news';

but then we don't have any identifier and this might overwrite any previous two parameters structure you have

example.com/()/()/

but you can try it (remember to save permalinks)

2012-10-18

John Cotton answers:

Is north a real page (ie one that you are managing content in through the dashboard)?

If not, why not make a custom taxonomy for your fruit type with north, south etc as entries?


docz comments:

Yes, north is a real page, and I want to be able to edit it using "pages" in dashboard.


John Cotton comments:

I which case it's problem laden since your north page could have child pages and that's going to make picking up a reliable permalink harder.

You're suggestion to an earlier response - having a fix marker like 'fruit-type' in the custom permalink - is the way to go.


function my_rewrite_rules( $rules ) {
$newrules = array();

$newrules['^.*/fruit-type/([^/]+)$'] = 'index.php?fruit=$matches[1]&post_type=fruit';

return $newrules + $rules;
}
add_filter('rewrite_rules_array','my_rewrite_rules');

Save permalinks!


docz comments:

Super! That works!

Now all I need is to get the "permalink" to display correct on the dashboard admin page. Now it shows as: mysite.com/fruits/north/apple and not: mysite.com/north/fruits/apple


John Cotton comments:

Do you mean under the title bar when you're editing


docz comments:

yes


John Cotton comments:

Well, it's a completely different question, but I can tell you that it's a real pain and - personally - I've never managed to get it to work perfectly.


docz comments:

ok, I'll just set "rewrite" to false on the cpt, and remove the "permalink" edit feature.

Thank you so much for helping me!