I need help with structuring permalink for the custom post type that I made. I think I tried all the answers here. I'm hoping somebody can help me.
The custom permalink structure in the setting is set as
/%category%/%postname%/ and I would like my custom posts' permalink to be the same format. If the custom posts' category is
health then its permalink would be
localhost/health/post-title. I used
post_type_link filter to change the permalink.
And even after re-saving the permalink in the setting, the permalink would lead to 404.
This is how I setup and registered the custom post (in case I set the wrong arguments)
$args = array( 'labels' => $labels, 'public' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_icon' => 'dashicons-star-filled', 'query_var' => true, 'rewrite' => array('slug' => 'oa', 'with_front' => false), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 1, 'supports' => array('title', 'thumbnail', 'comments'), 'taxonomies' => array('category', 'post_tag'), 'show_in_rest' => true, 'rest_base' => 'oa-api', 'rest_controller_class' => 'WP_REST_Posts_Controller', );
From what I researched, it seems that I should use
add_rewrite_rule method. But I don't understand how rewrite works. Is this the right direction or am I missing something else?
I would like to solve this programmatically, not with a plugin.