Related
I have two custom post types. One with the slug artwork and another with the slug research-materials. The permalink structure for an artwork post is http://example.com/artwork/artwork-slug and this is expected, works fine and what I want. On the post.php page for any post that is of the research-materials post type I have a custom field (via ACF) that has the slug of the artwork post that the research materials post is related to and the desired slug for the research materials post. There's also a custom taxonomy set for the research-materials post type which would play a part in the construction of the permalink structure for the research-materials posts. The permalink would look like http://example.com/artwork/related-artwork-slug/research-materials/custom-taxonomy-term/research-materials-slug
Here's the code I have so far but I'm not getting the result I'd expect... instead I'm redirected to the artwork custom post type when I should be redirected tot he research-materials custom post type single page.
Any help appreciated. Thanks in advance.
class RRP{
public static function init(){
// set up custom post types and custom taxonomies
add_action( 'init', 'RRP::build_custom_post_types' );
add_action( 'init', 'RRP::build_custom_taxonomy' );
// set up custom fields
add_action( 'acf/init', 'RRP::register_custom_fields' );
// validate saved value in custom fields
add_filter( 'acf/validate_value', 'RRP::validate_saved_value_in_custom_fields', 10, 4 );
// update related artwork slug
add_filter( 'acf/update_value', 'RRP::update_related_artwork_slug', 10, 3 );
// add the research material type
add_action( 'save_post_research-materials', 'RRP::set_research_material_type' );
// build rewrite rules
add_action( 'init', 'RRP::rewrite_stuff', 10, 0 );
add_filter( 'query_vars', 'RRP::build_query_vars', 10 );
add_filter( 'pre_get_posts', 'RRP::pre_get_posts', 10 );
}
public static function build_custom_post_types(){
$labels = array(
'name' => 'Research Materials',
'singular_name' => 'Research Material',
'add_new' => 'Add New Research Material',
'add_new_item' => 'Add New Research Material',
'edit_item' => 'Edit Research Material',
'new_item' => 'New Research Material',
'view_item' => 'View Research Material',
'search_items' => 'Search Research Materials',
'not_found' => 'No Research Materials found',
'not_found_in_trash' => 'No Research Materials found in Trash',
'parent_item_colon' => 'Parent Research Material:',
'menu_name' => 'Research Materials',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array('research-material-type'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
'supports' => array(
'title', 'editor'
)
);
register_post_type( 'research-materials', $args );
}
public static function build_custom_taxonomy(){
$labels = array(
'name' => 'Research Material Types',
'singular_name' => 'Research Material Type',
'search_items' => 'Search Research Material Types',
'popular_items' => 'Popular Research Material Types',
'all_items' => 'All Research Material Types',
'parent_item' => 'Parent Research Material Type',
'parent_item_colon' => 'Parent Research Material Type',
'edit_item' => 'Edit Research Material Type',
'update_item' => 'Update Research Material Type',
'add_new_item' => 'Add New Research Material Type',
'new_item_name' => 'New Research Material Type Name',
'add_or_remove_items' => 'Add or remove Research Material Types',
'choose_from_most_used' => 'Choose from most used sfmomatheme',
'menu_name' => 'Research Material Type',
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_admin_column' => false,
'hierarchical' => false,
'show_tagcloud' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'query_var' => true,
'capabilities' => array(),
);
register_taxonomy( 'research-material-type', array( 'research-materials' ), $args );
$terms = get_terms('research-material-type');
if( empty($terms) ){
wp_insert_term( 'Document', 'research-material-type', array(
'slug' => 'document',
) );
}
}
public static function register_custom_fields(){
acf_add_local_field_group(array(
'key' => 'group_research_materials',
'title' => 'Research Material Custom Fields',
'fields' => array(
array(
'key' => 'field_research_materials_slug',
'type' => 'text',
'name' => 'research_materials_slug',
'label' => 'Research Material Slug',
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork',
'type' => 'relationship',
'name' => 'research_materials_related_artwork',
'label' => 'Related Artwork',
'post_type' => array(
'artwork',
),
'max' => 1,
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork_slug',
'type' => 'text',
'name' => 'research_materials_related_artwork_slug',
'label' => 'Related Artwork Slug',
'disabled' => 1,
'instructions' => 'Generated based on Related Artwork',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'research-materials',
),
)
),
));
}
public static function validate_saved_value_in_custom_fields($valid, $value, $field, $input){
if( empty($value) && $field['name'] !== 'research_materials_related_artwork_slug' ){
$valid = 'Field must not be empty!';
}
return $valid;
}
public static function set_research_material_type($post_id){
// build main term by default term or by whatever's the first result set
$terms = wp_get_object_terms( strval($post_id), 'research-material-type' );
$default_term = get_term_by( 'slug', 'document', 'research-material-type' );
if( empty($terms) ){
wp_set_object_terms( strval($post_id), $default_term->term_id, 'research-material-type' );
}
else{
wp_set_object_terms( strval($post_id), $terms[0]->term_id, 'research-material-type' );
}
}
public static function build_query_vars($vars){
$vars[] = 'research_materials_related_artwork_slug';
$vars[] = 'research_materials_slug';
$vars[] = 'research_materials_type';
return $vars;
}
public static function update_related_artwork_slug($value, $post_id, $field){
if( $field['name'] === 'research_materials_related_artwork_slug' ){
$value = get_field('research_materials_related_artwork', $post_id)[0]->post_name;
}
return $value;
}
public static function pre_get_posts($query){
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || !$query->is_main_query() ){
return;
}
$research_materials_related_artwork = get_query_var('research_materials_related_artwork_slug');
$research_materials_slug = get_query_var('research_materials_slug');
$research_materials_type = get_query_var('research_materials_type');
if( !empty($research_materials_related_artwork) && !empty($research_material_slug) && !empty($research_materials_type) ){
$meta_query = $query->get('meta_query');
if( empty($meta_query) ){
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '==',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '==',
)
);
}
else{
$meta_query[] = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '==',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '==',
)
);
}
$query->set('meta_query', $meta_query);
$tax_query = $query->get('tax_query');
if( empty($tax_query) ){
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
}
else{
$tax_query[] = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
}
$query->set('tax_query', $tax_query);
}
return $query;
}
public static function rewrite_stuff(){
add_rewrite_tag( '%research_materials_related_artwork_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_slug%', '([^&]+)' );
add_rewrite_rule( '^artwork/([^/]*)/research-materials/([^/]*)/([^/]*)/?', 'index.php?post_type=research-materials&research_materials_related_artwork_slug=$matches[1]&research_materials_slug=$matches[2]&research_materials_type=$matches[3]', 'top' );
}
}
RRP::init();
After much tinkering I figured it out. There were a lot of small things wrong with the above code and I also had to make use of the index_template hook. Here's the final code:
class RRP{
public static function init(){
// set up custom post types and custom taxonomies
add_action( 'init', 'RRP::build_custom_post_types' );
add_action( 'init', 'RRP::build_custom_taxonomy' );
// set up custom fields
add_action( 'acf/init', 'RRP::register_custom_fields' );
// validate saved value in custom fields
add_filter( 'acf/validate_value', 'RRP::validate_saved_value_in_custom_fields', 10, 4 );
// update related artwork slug
add_filter( 'acf/update_value', 'RRP::update_related_artwork_slug', 10, 3 );
// add the research material type
add_action( 'save_post_research-materials', 'RRP::set_research_material_type' );
// build rewrite rules
add_filter( 'index_template', 'RRP::point_artwork_and_artist_to_single_templates' );
add_action( 'init', 'RRP::rewrite_stuff', 0 );
add_filter( 'query_vars', 'RRP::build_query_vars', 10 );
add_filter( 'pre_get_posts', 'RRP::pre_get_posts', 10 );
}
public static function build_custom_post_types(){
$labels = array(
'name' => 'Research Materials',
'singular_name' => 'Research Material',
'add_new' => 'Add New Research Material',
'add_new_item' => 'Add New Research Material',
'edit_item' => 'Edit Research Material',
'new_item' => 'New Research Material',
'view_item' => 'View Research Material',
'search_items' => 'Search Research Materials',
'not_found' => 'No Research Materials found',
'not_found_in_trash' => 'No Research Materials found in Trash',
'parent_item_colon' => 'Parent Research Material:',
'menu_name' => 'Research Materials',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array('research-material-type'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array(
'slug' => 'research-materials',
'with_front' => false,
),
'capability_type' => 'post',
'supports' => array(
'title', 'editor'
)
);
register_post_type( 'research-materials', $args );
}
public static function build_custom_taxonomy(){
$labels = array(
'name' => 'Research Material Types',
'singular_name' => 'Research Material Type',
'search_items' => 'Search Research Material Types',
'popular_items' => 'Popular Research Material Types',
'all_items' => 'All Research Material Types',
'parent_item' => 'Parent Research Material Type',
'parent_item_colon' => 'Parent Research Material Type',
'edit_item' => 'Edit Research Material Type',
'update_item' => 'Update Research Material Type',
'add_new_item' => 'Add New Research Material Type',
'new_item_name' => 'New Research Material Type Name',
'add_or_remove_items' => 'Add or remove Research Material Types',
'choose_from_most_used' => 'Choose from most used sfmomatheme',
'menu_name' => 'Research Material Type',
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_admin_column' => false,
'hierarchical' => false,
'show_tagcloud' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'query_var' => true,
'capabilities' => array(),
);
register_taxonomy( 'research-material-type', array( 'research-materials' ), $args );
$terms = get_terms('research-material-type');
if( empty($terms) ){
wp_insert_term( 'Document', 'research-material-type', array(
'slug' => 'document',
) );
}
}
public static function register_custom_fields(){
acf_add_local_field_group(array(
'key' => 'group_research_materials',
'title' => 'Research Material Custom Fields',
'fields' => array(
array(
'key' => 'field_research_materials_slug',
'type' => 'text',
'name' => 'research_materials_slug',
'label' => 'Research Material Slug',
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork',
'type' => 'relationship',
'name' => 'research_materials_related_artwork',
'label' => 'Related Artwork',
'post_type' => array(
'artwork',
),
'max' => 1,
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork_slug',
'type' => 'text',
'name' => 'research_materials_related_artwork_slug',
'label' => 'Related Artwork Slug',
'disabled' => 1,
'instructions' => 'Generated based on Related Artwork',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'research-materials',
),
)
),
));
}
public static function validate_saved_value_in_custom_fields($valid, $value, $field, $input){
if( empty($value) && $field['name'] !== 'research_materials_related_artwork_slug' ){
$valid = 'Field must not be empty!';
}
return $valid;
}
public static function set_research_material_type($post_id){
// build main term by default term or by whatever's the first result set
$terms = wp_get_object_terms( strval($post_id), 'research-material-type' );
$default_term = get_term_by( 'slug', 'document', 'research-material-type' );
if( empty($terms) ){
wp_set_object_terms( strval($post_id), $default_term->term_id, 'research-material-type' );
}
else{
wp_set_object_terms( strval($post_id), $terms[0]->term_id, 'research-material-type' );
}
}
public static function build_query_vars($vars){
$vars[] = 'research_materials_related_artwork_slug';
$vars[] = 'research_materials_slug';
$vars[] = 'research_materials_type';
return $vars;
}
public static function update_related_artwork_slug($value, $post_id, $field){
if( $field['name'] === 'research_materials_related_artwork_slug' ){
$value = get_field('research_materials_related_artwork', $post_id)[0]->post_name;
}
return $value;
}
public static function pre_get_posts($query){
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || !$query->is_main_query() ){
return;
}
$research_materials_related_artwork = $query->get('research_materials_related_artwork_slug');
error_log(print_r($research_materials_related_artwork, true));
$research_materials_slug = $query->get('research_materials_slug');
error_log(print_r($research_materials_slug, true));
$research_materials_type = $query->get('research_materials_type');
error_log(print_r($research_materials_type, true));
if( !empty($research_materials_related_artwork) && !empty($research_materials_slug) && !empty($research_materials_type) ){
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '=',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '=',
)
);
$query->set('meta_query', $meta_query);
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
$query->set('tax_query', $tax_query);
}
return $query;
}
// needed because I also have a CPT called artwork
public static function point_artwork_and_artist_to_single_templates($templates = ''){
global $post;
if( $post->post_type == 'research-materials' ){
$templates = locate_template( 'single-research-materials.php' );
}
return $templates;
}
public static function rewrite_stuff(){
add_rewrite_tag( '%research_materials_related_artwork_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_type%', '([^&]+)' );
add_rewrite_rule( 'artwork\/(.*)\/research-materials\/(.*)\/(.*)\/?$', 'index.php?post_type=research-materials&research_materials_related_artwork_slug=$matches[1]&research_materials_type=$matches[2]&research_materials_slug=$matches[3]', 'top' );
}
}
RRP::init();
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
I have a custom post type that worked just fine earlier, but not anymore. I haven't installed any new plugins and my code seems perfectly valid. Other custom fields are showing (default post type), but the ones I'm talking about not anymore.
Here's the code:
// Show posts of 'post', 'homepage_slider' post types on home page
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'homepage_slider' ) );
return $query;
}
As you can guess, homepage_slider is my Custom Post Type. I haven't tweaked it's code one bit, but here it is just for reference:
function homepage_slider() {
$labels = array(
'name' => 'Images',
'singular_name' => 'Image',
'menu_name' => 'Homepage Slider Images',
'parent_item_colon' => 'Parent Image:',
'all_items' => 'All images',
'view_item' => 'View Image',
'add_new_item' => 'Add New Image',
'add_new' => 'Add New',
'edit_item' => 'Edit Image',
'update_item' => 'Update Image',
'search_items' => 'Search Image',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
);
$args = array(
'label' => 'homepage_slider',
'description' => 'Homepage Slider',
'labels' => $labels,
'supports' => array( 'title', 'thumbnail' ),
// 'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'myurl',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'page',
);
register_post_type( 'homepage_slider', $args );
}
// Hook into the 'init' action
add_action( 'init', 'homepage_slider', 0 );
I have queried both the fact that it is indeed the homepage and the main query is indeed running. Really weird error.
Any suggestions?
EDIT: I think something's wrong with the add_my_post_types_to_query function as it's not working with another Custom Post Type either.
Try replacing:
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'homepage_slider' ) );
return $query;
}
With:
function add_my_post_types_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'homepage_slider' ) );
return $query;
}
Okay, so here's how I fixed it. Not quite a natural fix, but it did the job.
I simply created a new loop that runs through the custom post type wherever it must be placed and repeat for every other custom field I have on the homepage.
:)
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;
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