0

I have a custom post type called ‘artist’. Whenever I create an ‘artist’ post I get the following permalink: www.myexample.com/artist/artist-name

This is my single-artist.php code:

<?php get_header(); ?>
    <?php $query = new WP_Query( 'post_type=artist' ); ?>
      <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <div class="artist-info">
            <p>Artist name</p>
            <a href="/hire-artist/artist-name">Hire artist</a>
        <div>
      <?php endwhile ?>
      <?php wp_reset_postdata(); ?> 
    <?php get_footer(); ?>

This is the code to register the custom post:

function codex_custom_init() {
    register_post_type( 'artist',
      array(
        'public' => true,
        'label'  => 'Artists',
        'menu_position' => 5,
        'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail' ),
      )
    ); 
}
add_action( 'init', 'codex_custom_init' );

I want to do that when an ‘artist’ post is created then also a ‘subpost’ of it is created using this permalink: www.myexample.com/hire-artist/artist-name

So both post remains associated but are different custom post type since I’ll be using another template for the ‘hire-artist’ sub post. Also the link in the a tag gets the link to the child custom post.

Is it possible to do? What would be the best way to do this automation?

2

Rather than create another post which appears to be for the sole purpose of rendering a different template, I would add a rewrite endpoint to support an additional segment at the end of artist URLs. In this case, your URLs will be:

www.example.com/artist/artist-name/hire/

The advantage of this is that all data pertaining to an artist can remain within the single artist post. When these posts are rendered on the front end, the queried object contains the artist data, no connection of multiple posts is necessary.

To accomplish this, we first add the endpoint:

function wpd_hire_endpoint(){
    add_rewrite_endpoint( 'hire', EP_PERMALINK );
}
add_action( 'init', 'wpd_hire_endpoint' );

Note that if your post type is hierarchical, you'll need to use the EP_PAGES endpoint mask instead of EP_PERMALINK.

Next, add a filter to single_template to load the hire template when those URLs are visited:

function wpd_hire_template( $template = '' ) {
    global $wp_query;
    if( ! array_key_exists( 'hire', $wp_query->query_vars ) ) return $template;

    $template = locate_template( 'hire.php' );
    return $template;
}
add_filter( 'single_template', 'wpd_hire_template' );

Remember to flush rewrite rules after adding the endpoint.

EDIT- adding an additional rewrite rule to achieve alternate URL structure:

add_rewrite_rule(
    'hire-artist/([^/]+)/?$',
    'index.php?artist=$matches[1]&hire=true',
    'top'
);
| improve this answer | |
  • Thanks Milo! it's just the client asked to do this way, with this permalink for SEO purposes. I will try this code but can you help me with an alternative to do the way I need to? thanks :) – wpuser Oct 21 '15 at 18:14
  • Maybe someone else can help. The other way is far more complicated and I don't have the time or energy to write all that code :) – Milo Oct 21 '15 at 18:20
  • Well not all the code but just what would be the way to do it I mean. So I have a reference to try, it's just I don't know where to start :/ – wpuser Oct 21 '15 at 18:23
  • I would just do it this way and add a rewrite rule to support the other URL structure. But I'd have to see evidence that you actually gain anything from an SEO perspective. – Milo Oct 21 '15 at 18:27
  • +1 for keeping everything in one post, URL is just an implementation detail (though might be tad nasty to get right for custom requirements). – Rarst Oct 21 '15 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.