Rewrite Rule for Post Meta

I would like to create an endpoint for a custom field. For example, I already have a custom 'competition' post type for which rewriting is working great and resolving at website.com/competitions/my-competition. Within that competition I have award categories created using Advanced Custom Fields, all of which have a unique ID.

I would like to be able to visit website.com/competitions/competition_slug/award-category/category_slug.

I've got as far as passing the unique ID of the category to the query vars and loading a custom template but am stuck on website.com/award-category/category_slug and can't figure out how to have my rewrite rule include the parent competition. Here's my code:

function aca_award_cat_rewrite_rule() {

    add_rewrite_rule( '^award-category/([^/]*)/?', 'index.php?award_category=$matches[1]', 'top' );
}

function aca_set_award_cat_query_var( $vars ) {

    array_push( $vars, 'award_category' );
    return $vars;
}

function aca_include_award_cat_template( $template ) {
    if( get_query_var('award_category') ) {
        $award_category_template = plugin_dir_path( dirname( __FILE__ ) ) . 'public/award-category-template.php';
        if( file_exists( $award_category_template ) )
            $template = $award_category_template;
    }
    return $template;
}

I realise that right now my rewrite_rule is sending the user via index.php but I can't figure out how to accommodate the awards/award_slug bit without interfering with the existing rewrite rules for the post type.

Topic query-variable rewrite-rules Wordpress

Category Web


Okay! So I got this working by just changing the rewrite_rule to include the event...

function aca_award_cat_rewrite_rule() {

    add_rewrite_rule( '^awards/([^/]*)/award-category/([^/]*)/?', 'index.php?competition=$matches[1]&award_category=$matches[2]', 'top' );
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.