1

example.com/wp-admin/edit-tags.php?taxonomy=fixed-tags&post_type=portfolio

Please how can I rewrite:

example.com/blog/fixed-tags/b2b-business-to-business

to

example.com/portfolio/{fixed-tag} - should throw 404 if tag does not exists

Example example.com/portfolio/b2b-business-to-business

and force to use a new template.

The objective is to list portfolio based on tags

9
  • Is /blog/ your WordPress installation directory? Then you probably can do example.com/blog/portfolio/b2b-business-to-business, not example.com/portfolio/b2b-business-to-business. I can be wrong. Commented Jul 8, 2021 at 23:46
  • Also, what exactly is "portfolio"? custom post type? Taxonomy? A Page? Commented Jul 8, 2021 at 23:47
  • site.test/wp-admin/edit.php?post_type=portfolio
    – Emeka Mbah
    Commented Jul 9, 2021 at 0:04
  • site.test/wp-admin/edit-tags.php?taxonomy=fixed-tags&post_type=portfolio
    – Emeka Mbah
    Commented Jul 9, 2021 at 0:06
  • And how exactly did you get "fixed-tags" custom taxonomy and "portfolio" custom post type? Is it from a theme? Because WordPress does not have those post types and taxonomy by default. Commented Jul 9, 2021 at 0:18

1 Answer 1

1

I ended up with this:

// Register Custom Taxonomy
function fixed_tags_taxonomy() {

    $labels = array(
        'name' => _x( 'Fixed Tags', 'Taxonomy General Name', 'text_domain' ),
        'singular_name' => _x( 'Fixed Tag', 'Taxonomy Singular Name', 'text_domain' ),
    );

    $args = array(
        'labels' => $labels,
        'hierarchical'=>true,
    );

    register_taxonomy( 'fixed-tags', array( 'portfolio' ), $args );
}
add_action( 'init', __NAMESPACE__ . '\\' . 'fixed_tags_taxonomy', 2 );

// rewrite url portfolio post type
// we want something in this format -> portfolio/{fixed_tag_name}  
// remember to visit wp-admin/options-permalink.php and click "save changes"
add_action('init', function() {

    add_rewrite_tag( '%portfolio%', '([^&]+)' );
    add_rewrite_rule( '^portfolio/([^/]*)/?', 'index.php?portfolio=$matches[1]&fixed_tags=$matches[1]','top' );

    // add_rewrite_tag( '%portfolio%' );
    add_rewrite_rule( '^portfolio$', 'index.php?pagename=portfolio','top' );

}, 10, 0);

// we need to register our query variable to wordpress 
// will pass it's value back to us when we call get_query_var('fixed_tags'))
add_filter( 'query_vars', function( $query_vars )
{
    $query_vars[] = 'fixed_tags';
    return $query_vars;
});

add_filter('template_redirect', function () 
{
    global $wp_query;

    $fixed_tag = get_fixed_tag(); 

    if ($fixed_tag) {
        status_header( 200 );
        $wp_query->is_404=false;

        $wp_query->set('post_type', 'portfolio');
        $wp_query->set('tax_query', [
            'relation' => 'AND',
                array(
                'taxonomy' => 'fixed-tags',
                'field'    => 'slug',
                'terms'    => array( $fixed_tag ),
            )
        ]);       
    }
});

add_filter( 'template_include', function ( $template ) 
{
    $fixed_tag = get_fixed_tag();

    if($fixed_tag){
        return locate_template( ['page-portfolio-flex-layouts.php' ] );
    }

    return $template;
});

function get_fixed_tag()
{
    $fixed_tag = sanitize_title(get_query_var('fixed_tags'));

    // we check if term is fixed tags
    // we tell wordpress to handle as 200 is found
    $term = get_term_by('slug', $fixed_tag, 'fixed-tags'); 

    return $term ? $fixed_tag : null;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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