AnswerBun.com

Add category to custom post URL

WordPress Development Asked by KWA on October 1, 2020

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)

One Answer

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.

Answered by deemi-D-nadeem on October 1, 2020

Add your own answers!

Related Questions

WordPress Importer “Failed to import Media”

0  Asked on January 19, 2021 by orbita

 

Override theme file from plugin

0  Asked on January 18, 2021 by toma-tomov

 

Why can I not view image attachment pages?

3  Asked on January 17, 2021 by h-hall

     

Image color changes in wordpress

0  Asked on January 16, 2021 by giulia

     

Inserting additional   into menu item text

2  Asked on January 16, 2021 by marsandback

 

wp_mail() not sending emails with ajax

2  Asked on January 16, 2021 by mohd-hasan

   

How to ignore fields if empty?

1  Asked on January 16, 2021 by israel-santiago

   

Change WordPress post-state in Admin Area

2  Asked on January 16, 2021 by gino

   

Select posts with any post_type from database?

1  Asked on January 15, 2021 by lisandro-vaccaro

 

Why does WP_Post not contain its permalink?

3  Asked on January 14, 2021 by dodov

   

How to check post type when using sanitize_title hook?

1  Asked on January 14, 2021 by michael-rogers

 

WordPress retrieve media except cropped

0  Asked on January 12, 2021 by adviner

 

You have an error in your SQL syntax – Help with query

2  Asked on January 11, 2021 by ogmios

   

Is it possible to display tag cloud widget by category?

1  Asked on January 11, 2021 by taller-de-wordpress

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP