• I’m trying to change my custom post types url by trying to include the custom taxonomy in the url

    for example i’d like my pages to be

    https://www.domain.com/food-%5Bcustom-taxonomy%5D/custompost

    so far i have the following:

    //// ARGUMENTS
    $args = array(

    ‘labels’ => $labels,
    ‘public’ => true,
    ‘publicly_queryable’ => true,
    ‘show_ui’ => true,
    ‘show_in_menu’ => true,
    ‘query_var’ => true,
    //’rewrite’ => true,
    ‘rewrite’ => array( ‘slug’ => ‘zzfood’, ‘with_front’ => false ),
    ‘capability_type’ => ‘post’,
    ‘has_archive’ => true,
    ‘hierarchical’ => false,
    ‘menu_position’ => 7,
    ‘supports’ => array(‘title’, ‘editor’, ‘custom-fields’, ‘excerpt’, ‘thumbnail’)

    —–

    register_post_type(‘property’, $args);

    —–

    register_taxonomy(‘property_type’, ‘property’, $args);

    —–

    add_filter(‘post_link’, ‘custom_taxonomy_permalink’, 10, 3);
    add_filter(‘post_type_link’, ‘custom_taxonomy_permalink’, 10, 3);

    function custom_taxonomy_permalink($permalink, $post_id, $leavename) {
    // Get post
    $post = get_post($post_id);
    if (!$post) return $permalink;

    if ($post->post_type == ‘property’) {
    // Get taxonomy terms
    $terms = wp_get_object_terms($post->ID, ‘property_type’);
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
    else $taxonomy_slug = ‘foodz’;
    return str_replace(‘zzfood’, $taxonomy_slug, $permalink);
    } else {
    return $permalink;
    }
    }

    but it doesnt work, it returns an empty array.

  • The topic ‘custom post type & taxonomy url rewrite’ is closed to new replies.