• Hi,

    I’m trying to rewrite a custom post type’s slug so that its taxonomy term appears first. So for example:

    post type: case_study
    taxonomy: sector
    terms: commercial, domestic

    so i want the url to end up being: website.com/domestic/case_study

    Is there an easy way to go about this? I can make it case_study/domestic easy enough but not the other way around.

    Apologies if this is simple stuff, quite new to this aspect of wordpress.

    Pete

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter peterorpete

    (@peterorpete)

    Hi i have managed to get the permalink looking correct now. But when i click it it doesnt show the post. Does anyone have any advice?

    Here is the code so far:

    add_action( ‘init’, ‘cptui_register_my_cpts’ );
    function cptui_register_my_cpts() {

    post type
    $labels = array(
    “name” => “Case Studies”,
    “singular_name” => “Case Study”,
    );

    $args = array(
    “labels” => $labels,
    “description” => “”,
    “public” => true,
    “show_ui” => true,
    “has_archive” => false,
    “show_in_menu” => true,
    “exclude_from_search” => false,
    “capability_type” => “page”,
    “map_meta_cap” => true,
    “hierarchical” => false,
    “rewrite” => array( “slug” => “%work_type%/case-studies”, “with_front” => true ),
    “query_var” => true,

    “supports” => array( “title”, “thumbnail”, “page-attributes”, “post-formats” ),
    “taxonomies” => array( “work” )
    );
    register_post_type( “case_studies”, $args );

    // End of cptui_register_my_cpts()
    }

    taxonomy

    add_action( ‘init’, ‘cptui_register_my_taxes’ );
    function cptui_register_my_taxes() {

    $labels = array(
    “name” => “Work Type”,
    “label” => “Work Type”,
    );

    $args = array(
    “labels” => $labels,
    “hierarchical” => false,
    “label” => “Work Type”,
    “show_ui” => true,
    “query_var” => true,
    “rewrite” => array( ‘slug’ => ‘%work_type%’, ‘with_front’ => false ),
    “show_admin_column” => false,
    );
    register_taxonomy( “work”, array( “testimonial”, “case_studies” ), $args );

    // End cptui_register_my_taxes
    }

    function to change permalink structure

    add_filter(‘post_type_link’, ‘work_permalink_structure’, 10, 4);
    function work_permalink_structure($post_link, $post, $leavename, $sample)
    {
    if ( false !== strpos( $post_link, ‘%work_type%’ ) ) {
    $work_type_term = get_the_terms( $post->ID, ‘work’ );
    $post_link = str_replace( ‘%work_type%’, array_pop( $work_type_term )->slug, $post_link );
    }
    return $post_link;
    }

    Thread Starter peterorpete

    (@peterorpete)

    hi still struggling on this, anybody got any ideas? Think i need to flush the permalinks, but i’m not sure how to get that working

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Rewrite page url so taxonomy term appears before custom post type’ is closed to new replies.