Archived topic
Hook doesn't work on CPT category archive
5 replies · Started by Tereza on December 10, 2020
Hello,
I have custom post type with some ACF. I would like to display this ACF under the title of CPT in Archive of this CPT and in the Category of this CPT. In Archive it works perfectly, but in category this hook is missing.
I add the these rules for location:
- CPT Category - 'name of the category'
- CPT Archive
- CPT Category Archive - 'name of the category'
I use the hook called 'generate_after_entry_title'
At first time I tried to create my own files for this CPT: archive-nameofcpt.php and content-nameofcpt.php, but the hook disappeared from Archive too, so I deleted it. Should I create some special files for category or something else?
I work on localhost for this time, but I try to upload it on some webhosting if you need to see it.
Thank you
And second thing is, that If I want to test it, and I pick the location of the hook as:
Post Category Archive - all categories
and press Update, this setting is automatically changed to CPT Category Archive and nothing is displayed. :(
Well, I found the cause of the problem. I use this filter in functions.php for displaying the CPT (named pedagog) in categories. If I delete it, the hook works in categories for Posts. But categories dont display the CPT. How to display the CPT in categories with working hooks, um..?
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', 'pedagog');
$query->set('post_type',$post_type);
return $query;
}
}
Hi there,
What exactly is that filter trying to do? May be able to offer an alternative.
Let me know :)
Hello Tom,
thank you for your reply. I searched how to rebuild the code of the filter above. I try this and now the hooks work :)
function custom_post_type_cat_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_category()) {
$query->set( 'post_type', array( 'post', 'pedagog' ) );
}
}
}
add_action('pre_get_posts','custom_post_type_cat_filter');
It displays the custom post types called 'pedagog' in category archive. Without this code I can add category to the CPT, but nothing is displayed on category archive. I am sorry for the interruption :)
Awesome, all working correctly now? :)