I have created the following custom post types:
- Artists
- Albums
- Songs
- Albums
Songs are children of Albums, Albums are children of Artists.
I am trying to set up a navigation structure that shows this relationship, in the following format: http://www.example.com/artists/alicia-keys/discography/girl-on-fire/brand-new-me
. alicia-keys
is a post name of type artists, girl-on-fire
is a post name for the post type albums & brand-new-me
is a post name for the post type Songs.
When someone shortens the url to say, http//www.example.com/artists/alicia-keys/discography
it should display albums that belong to Alicia Keys and if you simply type http://www.example.com/albums
it shows all albums from all artists. if I have a link within the Artists CPT e.g. 'See all albums', I want it to open a new page which displays all albums belonging to the parent artist
I have the following files:
- single-artists.php
- single-albums.php
- single-songs.php
- archive-artists.php
- archive-albums.php
- archive-songs.php
The question can be broken down as follows:
Is there a rewrite rule and/or filter code that I will need to write in functions.php? if so, how do i go about it?
do I need to change/rewrite the slug? if so, what do I need to do
Will I need additional files to achieve the above link structure? if yes please explain.
Just a note: I am using Wp-types plugin to create the custom post types however, I strongly feel this problem is more of a Wordpress challenge rather than one restricted to a specific plugin. After all, I believe the plugin produces almost the same code that you would get if you were to code the post types from scratch but nonetheless, I have also posted a similar question in their forum. If I get an answer there, I will also supply it here to help anyone else who might face such a problem in the future.
UPDATE: Potential path to solution
After some googling I was able to find a path that I believe will lead to solving the problem. I was able to create the following link structure, which loads all albums for alicia keys :
- example.com/artists/alicia-keys/discography .
The code below is taken from a tutorial by Tareq Hasan and modified for the example. see the original here - http://code.tutsplus.com/articles/custom-page-template-page-based-on-url-rewrite--wp-30564
As the original author of the code states, this requires PHP 5.3.0 and up due to use of PHP anonymous functions.
in functions.php
<?php
//create a rewrite rule that will append /discography to artists endpoint
function ex_rewrite(){
//add_rewrite_rule( 'artists/([^/]+)/discography', 'index.php?artists=$matches[1]&discography=yes', 'top' );
add_rewrite_rule( 'artists/([^/]+)/discography/([^/]+)/?', 'index.php?album_artist=$matches[1]&discography=yes&albums=$matches[2]', 'top' );
}
add_action('init','ex_rewrite');
//register query variable so that wordpress recognizes discography query_var
function ex_query_var($vars){
$vars[]= 'discography';
$vars[] = 'album_artist';
return $vars;
}
add_filter('query_vars','ex_query_var');
//tell wordpress that when it sees the discography query variable,set/load a template called single-artists-discography.
add_filter( 'template_include', function( $path ) {
if ( get_query_var( 'album_artist' ) && is_singular( 'artists' ) ) {
return get_template_directory() . '/single-artists.php';
}
if ( get_query_var( 'discography' ) && is_singular( 'artists' ) ) {
return get_template_directory() . '/single-artists-discography.php';
}
return $path;
});
The single-artists-discography holds the logic of displaying all albums belonging to the specific artist. in this case, it will display all albums that belong to Alicia Keys. This answers part three of the question breakdown above, the need for additional files.
So the challenge narrows down to this:
- How to rewrite an Album's URL to display as example.com/artists/alicia-keys/discography/girl-on-fire.
- How to rewrite a song's URL to display as example.com/artists/alicia-keys/discography/girl-on-fire/brand-new-me
I hope now more knowledgeable developers can now use this as a pointer to help me figure out how to solve the above,as I research more.