Wordpress Custom Type permalink with custom Taxonomy slug - wordpress

I have a custom post type on my site called Homes, with a custom taxonomy called homes-category. They are both working correctly, but I am having trouble with the permalinks. I need the permalink to be homes/homes-category/pagename. I wrote a rewrite function, so the permalink is showing up correctly whenever I go to a homes item, but I get a 404 on the page itself. I am not sure what is causing this and have no ideas on how to fix it. The homes items work fine without the custom taxonomy in the permalink, but not with it. Does anyone have any ideas on how to fix this? I've been searching for days with no luck.
Here is the code for my custom post type:
register_post_type( 'Homes',
array(
'labels' => array(
'name' => __( 'Homes' ),
'singular_name' => __( 'Homes Item' ),
'add_new_item' => __('Add New Homes Item'),
'edit_item' => __('Edit Homes Item'),
'new_item' => __('New Homes Item'),
),
'supports' => array('title', 'thumbnail', 'editor'),
'taxonomies' => array('homes-category'),
'public' => true,
'has_archive' => false,
'show_in_nav_menus' => TRUE,
'show_in_menu' => TRUE,
'rewrite' => array(
'slug' => 'homes/%homes-category%',
'with_front' => true,
'hierarchical' => true,
),
)
);
Here is the code for my custom taxonomy
register_taxonomy(
'homes-category',
'homes',
array(
'hierarchical' => true,
'label' => __( 'Availability Category' ),
'rewrite' => array(
'slug' => 'homes',
'with_front' => false,
),
)
);
And here is the rewrite function
function custom_post_link($post_link, $id = 0)
{
$post = get_post($id);
if(!is_object($post) || $post->post_type != 'homes')
{
return $post_link;
}
$homes = 'misc';
if($terms = wp_get_object_terms($post->ID, 'homes-category'))
{
$client = $terms[0]->slug;
//Replace the query var surrounded by % with the slug of
//the first taxonomy it belongs to.
return str_replace('%homes-category%', $homes, $post_link);
}
//If all else fails, just return the $post_link.
return $post_link;
}
add_filter('post_type_link', 'custom_post_link', 1, 3);

You need to add rewrite_tag for, '%homes-category%'
Even you need to add, custom rewrite_rule, to interpret it correctly.
You can use below code,
add_action('init','wdm_register_post_types');
function wdm_register_post_types()
{
global $wp_rewrite;
register_post_type( 'Homes',
array(
'labels' => array(
'name' => __( 'Homes' ),
'singular_name' => __( 'Homes Item' ),
'add_new_item' => __('Add New Homes Item'),
'edit_item' => __('Edit Homes Item'),
'new_item' => __('New Homes Item'),
),
'supports' => array('title', 'thumbnail', 'editor'),
'taxonomies' => array('homes-category'),
'public' => true,
'has_archive' => false,
'show_in_nav_menus' => TRUE,
'show_in_menu' => TRUE,
'rewrite' => array(
'slug' => 'homes/%homes-cat%',
'with_front' => true,
'hierarchical' => true
),
)
);
register_taxonomy(
'homes-category',
'homes',
array(
'hierarchical' => true,
'label' => __( 'Availability Category' ),
'rewrite' => array(
'slug' => 'homes-category',
'with_front' => true,
),
)
);
$wp_rewrite->add_rewrite_tag( '%homes-cat%', '[a-zA-Z0-9_]');
add_rewrite_rule('^homes/([^/]+)/([^/]+)/?$','index.php?homes=$matches[2]','top');
}
function wp_tuts_filter_post_link( $permalink, $post ) {
if ( false === strpos( $permalink, '%homes-cat%' ) )
return $permalink;
$terms = current(wp_get_post_terms( $post->ID, 'homes-category', array('fields' => 'slugs')));
$permalink = str_replace( '%homes-cat%', $terms , $permalink );
return $permalink;
}
add_filter( 'post_link', 'wp_tuts_filter_post_link' , 10, 2 );
add_filter('post_type_link','wp_tuts_filter_post_link' , 10, 2 );
This will interpret,
http://domain.com/homes/2-bhk/home-1/
properly.
For more details you can also check out this blog post on Creating Custom WordPress Rewrite Rules for Pretty Permalinks

Related

How to add 2 different taxonomies to Wordpress CPT permalink structure

I have problem to add a second taxonomy to my cpt permalink structure.
With one register_taxonomy (pxlr-products-category) everything works fine and looks like this: https://example.com/products/my_category/my_productname
But I want a second register_taxonomy (pxlr-products-status). When I do that I see in my permalink instead of the tax slug just this %status% https://example.com/products/my_category/%status%/my_productname and I get an 404 error. Of cause I'ved flushed my permalinks
A second thing is, (pxlr-products-status) can have a value like (discontinued) but it is not a must, it can be empty as well
My permalinks should look like this https://example.com/products/my_category/my_status/my_productname
Or look like this https://example.com/products/my_category/my_productname
Here is my CPT, I can't find my error and need help, thanks :-/
class PXLR_Products
{
function __construct(){
// register post type
add_action('init', array($this, 'register_products_post_type'));
// register taxonomy
add_action('init', array($this, 'register_products_taxonomy'));
// slug - category filter
add_filter('post_type_link', [$this,'products_post_link'], 1, 3 );
// Rewrite support
add_action('init', [$this, 'custom_rewrite_rules']);
}
function custom_rewrite_rules() {
add_rewrite_rule(
'products/(.+?)/?$',
'index.php?pxlr-products-category=$matches[1]',
'bottom');
// add_rewrite_rule(
// 'products/(.+?)/(.+?)/?$',
// 'index.php?pxlr-products-category=$matches[1]&pxlr-products-status=$matches[2]',
// 'bottom');
}
public function register_products_post_type()
{
$labels = array(
'name' => _x('Products', 'Post Type General Name', 'pxlr-products'),
'singular_name' => _x('Products', 'Post Type Singular Name', 'pxlr-products'),
'menu_name' => __('PXLR Products', 'pxlr-products'),
'all_items' => __('All Products', 'pxlr-products'),
'view_item' => __('Show product', 'pxlr-products'),
'add_new_item' => __('Add product', 'pxlr-products'),
'add_new' => __('Add Product', 'pxlr-products'),
'edit_item' => __('Edit product', 'pxlr-products'),
'update_item' => __('Update product', 'pxlr-products'),
'search_items' => __('Search products', 'pxlr-products'),
'not_found' => __('No products found', 'pxlr-products'),
'not_found_in_trash' => __('No products found in trash.', 'pxlr-products'),
);
$args = array(
'labels' => $labels,
'name' => 'Products',
'name_admin_bar' => 'Products',
'singular_name' => 'Products',
'menu_icon' => 'dashicons-microphone',
'label' => __('Products', 'pxlr-products'),
'hierarchical' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'show_in_nav_menus' => false,
'menu_position' => 28,
'supports' => array('title', 'editor', 'revisions', 'thumbnail', 'page-attributes', 'excerpt', 'custom-fields'),
'rest_base' => 'products',
'public' => true,
'show_ui' => true,
'has_archive' => 'products',
'can_export' => false,
'taxonomies' => array('pxlr-products-category', 'pxlr-products-status'),
'publicaly_queryable' => true,
'query_var' => true,
'exclude_from_search' => false,
//'rewrite' => ['slug' => _x('products/%category%', 'Post Type Slug', 'pxlr-products'), 'with_front' => true ], // for one taxonomy if working fine!!!!
'rewrite' => ['slug' => _x('products/%category%/%status%', 'Post Type Slug', 'pxlr-products'), 'with_front' => true ], // for two taxonomies
'template_lock' => 'all',
);
register_post_type('pxlr-products', $args);
}
public function products_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'pxlr-products-category' );
if( $terms ){
return str_replace( '%category%' , $terms[0]->slug , $post_link );
}
$terms = wp_get_object_terms( $post->ID, 'pxlr-products-status' );
if( $terms ){
return str_replace( '%status%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
public function register_products_taxonomy()
{
register_taxonomy(
'pxlr-products-category',
'pxlr-products',
array(
'label' => __('Categories Product', 'pxlr-products'),
'hierarchical' => true,
'with_front' => false,
'show_in_rest' => true,
'rewrite'=> ['slug' => _x('products', 'Category Slug', 'pxlr-products'),
'with_front' => false,
'hierarchical' => true
],
'show_admin_column' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
)
);
register_taxonomy(
'pxlr-products-status',
'pxlr-products',
array(
'label' => __('Status Product', 'pxlr-products'),
'hierarchical' => true,
'with_front' => false,
'show_in_rest' => true,
'rewrite'=> ['slug' => _x('products/%category%', 'Status Slug', 'pxlr-products'),
'with_front' => false,
'hierarchical' => true
],
'show_admin_column' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
)
);
}
}

kindly check my code how can i create separate category for my new custom post type

function create_post_type_opportunities() {
register_post_type( 'opportunities',
// CPT Options
array(
'labels' => array(
'name' => __( 'Opportunities'),
'singular_name' => __( 'Opportunities')
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'opportunities'),
'supports' => array('title','thumbnail','editor','icon' ),
)); }
// Hooking up our function to theme setup
add_action( 'init', 'create_post_type_opportunities');
/* Custom Post Type for our Add opportunities*/
here is my code kindly check my code how can i create separate category for my new custom post type
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Movies', 'twentythirteen' ),
'parent_item_colon' => __( 'Parent Movie', 'twentythirteen' ),
'all_items' => __( 'All Movies', 'twentythirteen' ),
'view_item' => __( 'View Movie', 'twentythirteen' ),
'add_new_item' => __( 'Add New Movie', 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'movies', 'twentythirteen' ),
'description' => __( 'Movie news and reviews', 'twentythirteen' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => true,
// This is where we add taxonomies to our CPT
'taxonomies' => array( 'category' ),
);
// Registering your Custom Post Type
register_post_type( 'movies', $args );
}
add_action( 'init', 'custom_post_type', 0 );
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'custom_post_type', 0 );
To display your custom post types on the same category page as your default posts, you need to add this code into your theme’s functions.php or a site-specific plugin.
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if( is_category() ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('nav_menu_item', 'post', 'movies'); // don't forget nav_menu_item `enter code here`to allow menus to work!
$query->set('post_type',$post_type);
return $query;
}
}

Adding Custom Taxonomies to all posts page

I've got a custom post type, Staff, and this post type has a custom taxonomy, Roles. I would like to be able to sort by/see roles on all staff page in the wp-admin backend like how categories and tags work by default for posts.
Thanks
Custom post type
function register_staff(){
$labels = array(
'name' => 'Staff',
'singular_name' => 'Staff',
'add_new' => 'Add Staff',
'all_items' => 'All Staff',
'add_new_item' => 'Add Staff Member',
'edit_item' => 'Edit Staff Member',
'new_item' => 'New Staff Member',
'view_item' => 'View staff',
'search_item' => 'Search staff',
'not_found' => 'No Items Found',
'not_found_in_trash' => 'No staff found in trash',
);
$args = array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'has_index' => true,
'has_archive' => true,
'publicly_queryable' => true,
'query_var' => true,
'has_archive' => true,
'rewrite' => true,
'capability_type' => "post",
'hierarchical' => false,
'supports' => array(
'title',
'editor',
'page-attributes',
'excerpt',
'thumbnail',
'revisions',
),
'taxonomies' => array("role"),
'menu_position' => 5,
'menu_icon' => "dashicons-businessperson",
'exclude_from_search' => false,
);
register_post_type('staff', $args);
}
add_action( "init", "register_staff");
Custom Taxonomy
add_action( "init", "register_staff");
function build_taxonomies() {
register_taxonomy('role', 'staff', array(
'label' => 'Roles',
'public' => true,
));
}
add_action( 'init', 'build_taxonomies', 0 );
See https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column
Untested like this:
if ( !function_exists('AddTaxColumn') ) {
function AddTaxColumn($cols) {
$cols['yourtaxonomy'] = __('My Taxonomy');
return $cols;
}
function AddTaxValue($column_name, $post_id) {
if ( 'yourtaxonomy' == $column_name ) {
$tax_id = wp_get_post_terms( $post_id, 'yourtaxonomy' );
if ($tax_id) {
$taxonomies = join( ", ", $tax_id );
echo $taxonomies;
}
}
}
add_filter( 'manage_team_posts_columns', 'AddTaxColumn' );
add_action( 'manage_team_posts_custom_column', 'AddTaxValue', 10, 2 );
add_filter( 'manage_edit-team_sortable_columns', 'AddTaxColumn' );
}

Custom Taxonomy Links For Custom Post Types Not Working

I have set up a custom post type and a custom taxonomy. Then I am displaying the list of taxonomies as a set of links so that if someone clicks on that link, it should bring up all the posts under that taxonomy. Currently this is not working. It keeps taking me to the 404 page with the 'This is somewhat embarrassing isn't it?' message.
Code is as follows:
FUNCTIONS.PHP
add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies() {
register_taxonomy( 'companies', 'companies', array( 'hierarchical' => true, 'label' => 'Company Categories', 'query_var' => true, 'rewrite' => true ) );
}
add_action('init', 'register_mypost_type');
function register_mypost_type() {
register_post_type('companies',array(
'labels' => array(
'name' => 'Companies',
'singular_name' => 'Company',
'add_new' => 'Add New Company',
'add_new_item' => 'Add New Company',
'edit_item' => 'Edit Company',
'new_item' => 'Add New Company',
'view_item' => 'View Company',
'search_items' => 'Search Companies',
'not_found' => 'No companies found',
'not_found_in_trash' => 'No companies found in trash'
),
'public' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt'),
'capability_type' => 'post',
'rewrite' => array('slug' => 'companies'),
'taxonomies' => array('category'),
'menu_position' => 7,
'has_archive' => true,
'hierarchical' => false
));
}
Then on another page called 'page-company.php' I use the following code to output the list of taxonomies as links:
<?php
$args = array( 'taxonomy' => 'companies' );
wp_list_categories( $args );
?>
When I hover over one of these links the URL is displayed as:
'http://localhost:81/?companies=graphic-design'
Graphic Design being one of the categories I have added to my custom taxonomy.
However clicking this link always takes me to the 404 page.
I have set up an archives page called archive-companies.php and I thought all of this would do the trick.
Any help that anyone can provide would be greatly appreciated.
Thanks in advance.
OMG OMG OMG ... after days reading posts on how to solve the issue, using rewrite rules and ussing permalinks rewriting code, your solution was the only one that worked perfectly!
The only change I needed to apply was in the custom taxonomy declaration:
This code
'rewrite' => array(
'slug' => 'pubs/type',
'with_front' => false
),
for this code
'rewrite' => true,
and that was it. Working like a charm!
Note:
Prior to my re-writing I tested your code and also got a 404.
1) I re-wrote you your custom post type and used your custom companies category.
2) Then I cycled from default to /%postname%/ and it works.
Functions.php
Here you go the custom post type:
// Register Custom Post Type
function register_mypost_type() {
$labels = array(
'name' => _x( 'Companies', 'Post Type General Name' ),
'singular_name' => _x( 'Company', 'Post Type Singular Name' ),
'menu_name' => __( 'Company' ),
'parent_item_colon' => __( 'Parent Company'),
'all_items' => __( 'All Companies'),
'view_item' => __( 'View Company'),
'add_new_item' => __( 'Add New Company'),
'add_new' => __( 'New Company'),
'edit_item' => __( 'Edit Company'),
'update_item' => __( 'Update Company' ),
'search_items' => __( 'Search companies' ),
'not_found' => __( 'No companies found' ),
'not_found_in_trash' => __( 'No companies found in Trash'),
);
$rewrite = array(
'slug' => 'company',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'company'),
'description' => __( 'Companies Posts' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', ),
'taxonomies' => array( 'companies' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 100,
'menu_icon' => '',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'query_var' => 'company',
'rewrite' => $rewrite,
'capability_type' => 'post',
);
register_post_type( 'company', $args );
}
add_action( 'init', 'register_mypost_type', 0 );
Your custom category
add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies() {
register_taxonomy( 'companies', 'companies', array( 'hierarchical' => true, 'label' => 'Company Categories', 'query_var' => true, 'rewrite' => true ) );
}
First of all create taxonomy-companies.php template in the root directory of your theme. This template will be responsible for displaying your taxonomy term posts.
then on that template you need to use the get_queried_object() to get all the taxonomy details.
e.g;
$queries_obj = get_queried_object();
echo '<pre>';
print_r( $queries_obj );
echo '</pre>';
it will return
WP_Term Object
(
[term_id] => 10
[name] => Featured companies
[slug] => featured-companies
[term_group] => 0
[term_taxonomy_id] => 10
[taxonomy] => companies-category
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
then query posts like below.
$q = new WP_Query( array(
'post_type' => 'companies', // post type name
'posts_per_page' => get_option( 'posts_per_page' ),
'tax_query' => array(
array(
'taxonomy' => $queries_obj->taxonomy,
'field' => 'term_id',
'terms' => array( $queries_obj->term_id )
)
)
) );
if ( $q->have_posts() ) :
while ( $q->have_posts() ) :
$q->the_post();
// loop do stuf
the_title();
endwhile;
wp_reset_query();
endif;

Trying to get both custom post type and posts to show up in tag and category pages

I've built a custom post type, that is set to use the out-of-the-box tags and categories that posts use. However, if I click on a tag or category link, the archive only shows posts with that tag, not my custom post type. I've tried a few ways to fix it but they don't seem to be working. My code is:
// Add Resource Post Type
add_action('init', 'hallam_init');
function hallam_init() {
// set up the labels
$labels = array(
'name' => _x('Resources', 'post type general name'),
'singular_name' => _x('Resource', 'post type singular name'),
'add_new' => _x('Add New', 'resource'),
'add_new_item' => __( 'Add New Resource' ),
'edit_item' => __( 'Edit Resource' ),
'new_item' => __( 'New Resource' ),
'view_item' => __( 'View Resource' ),
'search_items' => __( 'Search Resources' ),
'not_found' => __( 'No resources found' ),
'not_found_in_trash' => __( 'No respources found in Trash' ),
'parent_item_colon' => ''
);
// set up the args
$args = array (
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array (
'slug' => 'resources'
),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments'
),
'taxonomies' => array(
'collection',
'category',
'post_tag'
),
'has_archive' => true
);
register_post_type('ht_resource', $args);
}
// Add Taxonomy
register_taxonomy('collection', 'ht_resource', array(
'hierarchical' => true,
'label' => 'Collections',
'query_var' => true,
'rewrite' => true
));
// Fix the archives
add_filter( 'pre_get_posts', 'add_to_query' );
function add_to_query( $query ) {
// if ( is_home() ) {
if( $query->query_vars['suppress_filters'] ) // TODO check if necessary
return $query;
$supported = $query->get( 'post_type' );
if ( !$supported || $supported == 'post' )
$supported = array( 'post', 'ht_resource' );
elseif ( is_array( $supported ) )
array_push( $supported, 'ht_resource' );
$query->set( 'post_type', $supported );
return $query;
//}
}
Am I missing something obvious?
Can you plz try this by adding on your theme's functions.php? Hope it will work
function query_post_type($query) {
if(is_tag()) {
$query->set('post_type',$post_types=get_post_types('','names'));
return $query;
}
}
add_filter('pre_get_posts', 'query_post_type');
thnx

Resources