4

I am trying to create custom post with custom taxonomy and categories. Structure is like:

Product:

  • Has categories
  • Has taxonomy

Custom post type create:

function create_alco_custom_post() {
    $review_slug = 'reviews';

    $labels = array( 
        'name' => __( 'Recenzje' ),
        'singular_name' => __( 'Recenzja' ),
        'add_new' => __( 'Nowa recenzja' ),
        'add_new_item' => __( 'Dodaj nową recenzje' ),
        'edit_item' => __( 'Edytuj recenzje' ),
        'new_item' => __( 'Nowa recenzja' ),
        'view_item' => __( 'Zobacz recenzje' ),
        'search_items' => __( 'Szukaj w recenzjach' ),
        'not_found' =>  __( 'Nie znaleziono recenzji' ),
        'not_found_in_trash' => __( 'Nie znaleziono recenzji w koszu' ),
    );
    $args = array(
        'labels' => $labels,
        'menu_icon' => 'dashicons-format-quote',
        'public' => true,
        'has_archive' => true,
    'rewrite' => array('slug' => '%review-category%','with_front' => true),
        'hierarchical'  => true,
        'menu_position' => 0,
        'exclude_from_search' => false,
        'supports' => array(
            'title', 
            'editor',
            'thumbnail'
        ),
        'taxonomies' => array('category')
    );
    register_post_type( 'review', $args );
} 
add_action( 'init', 'create_alco_custom_post' );

Then I add taxonomy:

$labels = array(
    'name'                          => __( 'Producenci', 'alkowiki' ),
    'singular_name'                 => __( 'Producent', 'alkowiki' ),
    'search_items'                  => __( 'Wyszukaj wśród producentów', 'alkowiki' ),
    'popular_items'                 => __( 'Popularni producenci', 'alkowiki' ),
    'all_items'                     => __( 'Wszystcy producenci', 'alkowiki' ),
    'parent_item'                   => __( 'Producent rodzic', 'alkowiki' ),
    'edit_item'                     => __( 'Edytuj producenta', 'alkowiki' ),
    'update_item'                   => __( 'Aktualizuj producenta', 'alkowiki' ),
    'add_new_item'                  => __( 'Dodaj nowego producenta', 'alkowiki' ),
    'new_item_name'                 => __( 'Nowy producent', 'alkowiki' ),
    'separate_items_with_commas'    => __( 'Oddziel producentów przecinkami', 'alkowiki' ),
    'add_or_remove_items'           => __( 'Dodaj lub usuń producentów', 'alkowiki' ),
    'choose_from_most_used'         => __( 'Wybierz z najczęściej wykorzystywanych producentów', 'alkowiki' )
);

$args = array(
    'labels'                        => $labels,
    'public'                        => true,
    'hierarchical'                  => true,
    'show_ui'                       => true,
    'show_in_nav_menus'             => true,
    'query_var'                     => true
);

register_taxonomy( 'producents', 'review', $args );

And finally I change link structure:

add_filter('post_link', 'category_permalink', 1, 3);
add_filter('post_type_link', 'category_permalink', 1, 3);

function category_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%review-category%') === FALSE) return $permalink;
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'category');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
            $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'nieskateryzowane';

    return str_replace('%review-category%', $taxonomy_slug, $permalink);
}

My post structure in WP settings is /%category%/%postname%/

And post URL's are created properly, category archives are displaying posts properly, permlinks are created as I wanted, but when I go to product site (URL structure site/category/product-name) I receive 404. When I visit URL with taxonomy instead of category (site/taxonomy/product-name) I'm being redirected to /category/product-name site, and it also ends with 404.

I'm struggling with this for days, any ideas?

EDIT:

Unfortunately I had to switch back permalinks to default (%postname%) - I want default WP Posts to have URL structure like this. I've also change default category (which is connected with default posts) to custom taxonomy called "my-category", and register it for my custom post type. So now there are 2 taxonomies, (same as first one inserted). Archive pages for them gives 404, and product page also still gives 404. How should I set rewrite structure?

I'll precise what I want to achieve:

  • site.com/%taxonomy1%/ - archive for custom post with this taxonomy
  • site.com/%taxonomy2%/ - archive for custom post with this taxonomy
  • site.com/%taxonomy1%/custom-post-name/ - custom-post page (link only with first taxonomy)
  • The root of the problem is that, by default, you can't have multiple types of requests that share the same URL pattern- WordPress doesn't know what taxonomy or post type you're asking for. That's why category and tag archives have static slugs in their URL, and why custom types by default also have a static slug. – Milo Dec 30 '15 at 19:19
0

You can follow three steps for solution

1) Go to settings > permalinks and change the permalink as plane or default then save it.

2) Now Go to your site home page and refresh your home page. Now check the category pages.

3) Now go again settings > permalinks and now change it /%category%/%postname%/

And now go to home page refresh it and check your category pages

Hop it'll help you.

  • I've eddited my post – KWA Dec 30 '15 at 10:33
  • What is your wordpress version? – deemi-D-nadeem Dec 30 '15 at 11:15
  • WordPress 4.4. I've tested your solution and it doesn't work. – KWA Dec 30 '15 at 11:19
  • You change the permalink structure as plan? – deemi-D-nadeem Dec 30 '15 at 11:21
  • Yeap, tried with basic, tried with edited - nothing works. Tried about 20 configurations, when custom-post works - pure pages have 404, when pure pages works - custom posts have 404... – KWA Dec 30 '15 at 11:28

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.