0

I am trying to get the URL working for custom post type "our-work" without success. When I created custom post type (our-work) post and assign to custom category sustainability and check the post to be not found. The url address does not appear to be like this our-work/sustainability/project-post-name but our-work/project-name-project. Could anyone correct me on this code and why it not working?

// Project Custom Post
 function create_post_type() {
    register_post_type( 'our-work',
        array(
            'labels' => array(
                'name'=> _('Our Work'),
                'singular_name' => _('Our Work')
             ),
            'public' => true,
            'has_archive' => true,
            'taxonomies'  => array( 'projects' ),
            'hierarchical'        => true,
            'capability_type'     => 'post',
             'publicly_queryable'  => true,
            'show_in_nav_menus'   => true,
             'show_in_menu'        => true,
            'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', ),
        )
    );
}
add_action( 'init', 'create_post_type' );

//hook into the init action and call create_book_taxonomies when it fires
 
add_action( 'init', 'create_subjects_hierarchical_taxonomy', 0 );
 
//create a custom taxonomy name it subjects for your posts
 
function create_subjects_hierarchical_taxonomy() {
 
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
 
  $labels = array(
    'name' => _x( 'Projects', 'taxonomy general name' ),
    'singular_name' => _x( 'Project', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Project' ),
    'all_items' => __( 'All Projects' ),
    'parent_item' => __( 'Parent Project' ),
    'parent_item_colon' => __( 'Parent Project:' ),
    'edit_item' => __( 'Edit Subject' ), 
    'update_item' => __( 'Update Subject' ),
    'add_new_item' => __( 'Add New Subject' ),
    'new_item_name' => __( 'New Subject Name' ),
    'menu_name' => __( 'Projects' ),
  );    
 
// Now register the taxonomy
  register_taxonomy('projects',array('our-work'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'our-work' ),
  ));
 
}

1 Answer 1

0

why it not working

Because your post type's permalink structure (which defaults to our-work/<post name/slug>) does not contain the category.

So if you want the permalink structure for your post type (our-work) be our-work/<category>/<post name/slug>, here's one way that works in WordPress 5.6 & 5.5, but uses just one of the post categories:

  1. Add the <category> part to the post type's rewrite slug — here we're using %projects% as the category placeholder, but you can use other tag like %project_cats%:

    register_post_type( 'our-work', array(
        'rewrite' => array( 'slug' => 'our-work/%projects%' ),
        // your other args
    ) );
    
  2. Use the post_type_link hook to replace the above part (i.e. %projects%) with the actual category slug:

    add_filter( 'post_type_link', 'my_post_type_link', 10, 2 );
    function my_post_type_link( $post_link, $post ) {
        if ( $post && 'our-work' === $post->post_type ) {
            // Get all the "projects" terms assigned to the post.
            $terms = get_the_terms( $post, 'projects' );
    
            // But use only the first term ($terms[0]) which is normally the most recent
            // term assigned to the post. So if you assigned it the terms Foo and later
            // Bar, $terms[0] would be the Bar term.
            $post_link = str_replace( '%projects%', $terms[0]->slug, $post_link );
        }
    
        return $post_link;
    }
    

That's all, and be sure to flush the rewrite rules (just visit the permalink settings page).

7
  • Thank Sally for your suggestion, what if the post does not assign to category? Is there permalink conditional to whether post assign to projects or not? Commented Dec 14, 2020 at 1:53
  • Then you can use any word/text as the category replacement. E.g. $post_link = str_replace( '%projects%', empty( $terms ) ? 'all-projects' : $terms[0]->slug, $post_link ); - so all-projects in this case becomes the default "category" slug used for "completing" the post permalink.
    – Sally CJ
    Commented Dec 14, 2020 at 2:09
  • I do not want it part of category if the custom post are not assign to category, sorry I have limit knowledge in php language. When I click Our Work from the navigation the link appear to include %projects% at the end and the child links include category instead of %projects%. I am not sure what went wrong? Commented Dec 14, 2020 at 2:18
  • Where did you put the second code in my answer? It should be added to the bottom of your code. I.e. Add the add_filter( ... ); function my_post_type_link(... after the function create_subjects_hierarchical_taxonomy(....
    – Sally CJ
    Commented Dec 14, 2020 at 2:27
  • "Is there permalink conditional to whether post assign to projects" - yes, the post should have a projects term/category. But as I said, for posts without that category, you could use the all-projects or another unique slug.
    – Sally CJ
    Commented Dec 14, 2020 at 2:33

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.