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

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

Related

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;
}
}

WordPress Custom Type append Post ID at the end

I've been searching for two days know and tried almost every example I've seen online but the real problem is that all examples have different post-type-names or want different results in general.
That's why I want to ask for help and start from scratch.
I've created a Custom Post Type for listing some Jobs opportunities but because most job-titles have the same name (product designer) I would like to prevent WordPress from adding -1, -2, -3, -4 after each slug over time and that's why I was thinking about adding the %post_id% behind the %post-name% for only this new custom post type.
A post_id should be an unique number so that would prevent ugly urls in the long run.
This is what I have, only the post type setup, but right now I think I need to work with rewrites and stuff.
function jobs() {
$labels = array(
'name' => _x( 'Jobs', 'Post Type General Name', 'theme-name' ),
'singular_name' => _x( 'Job', 'Post Type Singular Name', 'theme-name' ),
'menu_name' => __( 'Jobs', 'theme-name' ),
'parent_item_colon' => __( 'Parent Job:', 'theme-name' ),
'all_items' => __( 'All Jobs', 'theme-name' ),
'view_item' => __( 'View Job', 'theme-name' ),
'add_new_item' => __( 'Add New Job', 'theme-name' ),
'add_new' => __( 'Add New', 'theme-name' ),
'edit_item' => __( 'Edit Job', 'theme-name' ),
'update_item' => __( 'Update Job', 'theme-name' ),
'search_items' => __( 'Search Job', 'theme-name' ),
'not_found' => __( 'Not found', 'theme-name' ),
'not_found_in_trash' => __( 'Not found in Trash', 'theme-name' ),
);
$rewrite = array(
'slug' => 'vacatures',
'with_front' => false,
'pages' => false,
'feeds' => false,
);
$args = array(
'label' => __( 'vacatures', 'theme-name' ),
'description' => __( 'Post Type Description', 'theme-name' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_in_admin_bar' => false,
'menu_position' => 9,
'menu_icon' => 'dashicons-welcome-learn-more',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
);
register_post_type( 'vacatures', $args );
}
// Hook into the 'init' action
add_action( 'init', 'jobs', 0 );
any help would be so thankful!
EDIT
I'm a bit further now and I have a feeling I'm getting close to what I need.
I've added the code below and the result is almost good. It appends the post_id on the end of the slug only problem it's twice/double.
I want this:
http://example.com/vacatures/product-designer/123/
But instead it does this:
http://example.com/vacatures/product-designer/123/123/
function fix_permalink( $post_link, $id = 0 ) {
add_rewrite_rule(
'^vacatures/',
'index.php?post_type=vacatures&p=',
'top'
);
$post = get_post($id);
if ( is_wp_error($post) || $post->post_type != 'vacatures' ) {
return $post_link;
}
empty ( $post->slug )
and $post->slug = sanitize_title_with_dashes( $post->post_title );
return home_url( user_trailingslashit( "vacatures/$post->slug/$post->ID" ) );
}
add_filter( 'post_type_link', 'fix_permalink' );
Looking at your code, it looks as though you've rewritten the slug to imitate the post ID and yet in the last line of code you state the new permalink should be the post_type followed by the slug (which is now the ID) followed by the ID (the reason for the double).
I did a few tests and indeed either removing the slug or ID from the new permalink structure it did remove the duplicate. But I could not get the_permalink() to correctly link to a single view of my custom post type post (Eg. within an archive page using 'the_permalink()' did not link to my single.php for that post type), so I cannot say that I could get your code to work for me.
But I have been using this snippet for a while and it does what you'd like.
Your rewrite:
'rewrite' => array( 'slug' => 'vacatures' )
The function:
// Rewrite permalink structure
function vacatures_rewrite() {
global $wp_rewrite;
$queryarg = 'post_type=vacatures&p=';
$wp_rewrite->add_rewrite_tag( '%cpt_id%', '([^/]+)', $queryarg );
$wp_rewrite->add_permastruct( 'vacatures', '/vacatures/%cpt_id%/', false );
}
add_action( 'init', 'vacatures_rewrite' );
function vacatures_permalink( $post_link, $id = 0, $leavename ) {
global $wp_rewrite;
$post = &get_post( $id );
if ( is_wp_error( $post ) )
return $post;
$newlink = $wp_rewrite->get_extra_permastruct( 'vacatures' );
$newlink = str_replace( '%cpt_id%', $post->ID, $newlink );
$newlink = home_url( user_trailingslashit( $newlink ) );
return $newlink;
}
add_filter('post_type_link', 'vacatures_permalink', 1, 3);
New permalinks structure is: http://domain.com/vacatures/208/
I hope this helps you.

Wordpress Custom Type permalink with custom Taxonomy slug

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

Custom post type won't display, IS homepage & main query

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.
:)

Wordpress paginate_links() function, 404s and trouble

This has been a horrendous, crippling, time stealing (two week) horrible horrible problem.
It might be rewriting, my use of custom posts, but after stripping out so much (and reducing some functionality of my theme) I've reduced my problem to this:
paginate_links() is putting out a link like this:
?s=cute&post_type=image&paged=2
When I changed the variable in the browser bar to page=2 (dropping the 'd').
?s=cute&post_type=image&page=2
It works correctly.
So my question reduced to this: If I must get that function to output the "page" variable properly, how is that done?
paged and paged are both used by WordPress. So conversely, how do I get paged recognized if that is better practice?
For all I know, this is indicative of some deeper issue I have in my theme, but for the life of me I can't see how else I could be going wrong!
EDIT:
Here is my code I'm using:
if ( get_query_var('paged') )
$paged = get_query_var('paged');
elseif ( get_query_var('page') )
$paged = get_query_var('page');
else
$paged = 1;
$big = 999999999; // need an unlikely integer
$stuff_to_echo = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?page=%#%',
'current' => max( 1, $paged ),
'total' => $wp_query->max_num_pages,
'type' => 'array'
) );
EDIT 2 - If above is good, here is another possible area the problem might be happening. I'll post the creation of the custom post type and the rewrite rules -
//image custom post type
add_action( 'init', 'symbiostock_image_manager_register' );
function symbiostock_image_manager_register( )
{
//creating custom post type for image
$labels = array(
'name' => 'Symbiostock Images',
'singular_name' => 'Image',
'add_new' => 'New Image',
'add_new_item' => 'Add New Image',
'edit_item' => 'Edit Image',
'new_item' => 'New Image',
'all_items' => 'All Images',
'view_item' => 'View Image',
'search_items' => 'Search Images',
'not_found' => 'No image found',
'not_found_in_trash' => 'No images found in Trash',
'parent_item_colon' => '',
'menu_name' => 'RF Images'
);
$args = array(
'labels' => $labels,
'singular_label' => __( 'Image' ),
'description' => 'Image Listings',
'menu_position' => 100,
'menu_icon' => symbiostock_IMGDIR . '/symbiostock_icon2.png',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'exclude_from_search' => false,
'supports' => array(
'title',
'editor',
'thumbnail'
),
'rewrite' => array(
'slug' => 'image',
'with_front' => false
)
);
register_post_type( 'image', $args );
register_taxonomy( 'image-type', array(
'image'
), array(
'hierarchical' => true,
'label' => 'Image Categories',
'singular_label' => 'Image Type',
'rewrite' => true,
'exclude_from_search' =>false,
'public' => true,
'slug' => 'image-type'
) );
register_taxonomy( 'image-tags', array(
'image'
), array(
'hierarchical' => false,
'rewrite' => true,
'query_var' => true,
'singular_label' => 'Image Keyword',
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'slug' => 'image-tag',
'labels' => array(
'name' => _x( 'Image Keywords', 'taxonomy general name' ),
'singular_name' => _x( 'Keywords', 'taxonomy singular name' ),
'search_items' => __( 'Search Images' ),
'all_items' => __( 'All Image Keywords' ),
'edit_item' => __( 'Edit Image Keyword' ),
'update_item' => __( 'Update Image Keyword' ),
'add_new_item' => __( 'Add New Image Keyword' ),
'new_item_name' => __( 'New Keyword Name' ),
'menu_name' => __( 'Image Keywords' ),
),
'rewrite' => array(
'slug' => 'search-images', // This controls the base slug that will display before each term
'with_front' => false,
'hierarchical' => false
),
) );
}
add_action( 'admin_init', 'symbiostock_image_directory' );
function symbiostock_image_directory( )
{
add_meta_box( 'symbiostock-image-meta', 'Symbiostock Image Info', 'symbiostock_image_manager_meta_options', 'image', 'normal', 'high' );
}
And here are the rewrite rules further down -
add_action( 'init', 'symbiostock_rewrite' );
function symbiostock_rewrite( )
{
global $wp_rewrite;
$wp_rewrite->add_permastruct('typename','typename/%year%%postname%/' , true , 1);
add_rewrite_rule('typename/([0-9]{4})/(.+)/?$','index.php?typename=$matches[2]', 'top');
$wp_rewrite->flush_rules();
}
I know this is old but I've had the same problem and solved it by changing the permalink of the page that was causing the 404.
This is because, apparently, you cannot have a page-slug with the same name as your custom post type.
All credit goes to Ryan S. for sharing the original solution: http://www.sutanaryan.com/2013/09/404-error-in-custom-post-type-pagination-wordpress/
Under your args for paginate_links() I would ensure that you have your format set properly. 'paged' is typically used for obtaining the "current page" the user is on (Reference: https://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query)
'format' => '?page=%#%'
it sounds like you have it set to '?paged=%#%'
Here is a complete example
global $wp_query;
$args = array(
'format' => '?page=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
);
paginate_links($args);
It might not be the most elegant solution, but is appropriate in this case.
I simply set up a redirect to the taxonomy page in the event someone is searching this post type.
add_action( 'parse_query', 'image_search_redirect' );
function image_search_redirect( $query ) {
if ( ( is_search() && get_query_var( 'post_type' ) == 'image' ) ) {
wp_redirect(home_url("?image-tags=") . urlencode(get_query_var('s')));
exit();
}
}
This is not as much a solution as a creative work-around. There should be no reason why I get a 404 not found on the "paged=" variable. If some future person knows a better solution I'd love to know one!

Resources