- This topic has 19 replies, 4 voices, and was last updated 3 years, 2 months ago by Sampat Jangir.
-
AuthorPosts
-
December 17, 2020 at 5:25 pm #1585117Goran Dragutinovic
The problem:
– Custom post type messes up the normal display of my category and tag archive structure
– Instead of 2 column structure and meta being displayed as: img > category link > title > author
– It gives each entry it’s own line and the meta is reduced to: img > category > title
– Also, on category and tag archives, the thumbnail from entry from the custom post is showing up as around 1.5x bigger than others + left-aligned, and the meta is to the right.
– But all the other category archive entries have the img on top of the other meta
– note on the above: I do have a code in my function.php to “add avatar to author byline” but removing it does nothing
– custom post types share categories and tags with default post types.Things I’ve done so far:
– created a custom post type
function my_custom_post() { $args = array( 'labels' => array( 'name' => 'My CPTS', 'singular_name' => 'My CPT', ), 'hierarchical' => false, 'public' => true, 'has_archive' => true, 'menu_icon' => 'dashicons-universal-access', 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'author', 'custom-fields', 'comments', 'revisions', 'post-formats' ), 'taxonomies' => array('category', 'post_tag'), 'show_in_rest' => true //'rewrite' => array('slug' => 'cpt'), <- I could make this whatever I want it to say ); register_post_type('my-cpt', $args); } add_action('init', 'my_custom_post');
– hooked into the query to show custom post types in home and category archives
function add_cpt_to_category_archive( $query ) { if ( ! is_admin() && $query->is_main_query() ) { if ( is_archive() || is_category() || is_home() ) { // Add CPT to the category $query->set( 'post_type', array( 'post', 'my-cpt' ) ); } if ( $query->is_search() ) { // Add CPT to the search $query->set( 'post_type', array( 'post', 'my-cpt' ) ); } } } add_action('pre_get_posts', 'add_cpt_to_category_archive' );
What I’m looking to achieve:
– custom post types being fed into the category/home/search archives consistent with default post types layout/style
– custom post types and default post types to show up together in archives and search queriesDecember 18, 2020 at 6:27 am #1585816DavidStaffCustomer SupportHi there,
can you share a link to where we can see the problem?
December 18, 2020 at 9:29 am #1586211Goran DragutinovicSure, see pm.
The Test Post is the custom type one.
I’m also seeing that the custom post type archive includes non-custom post type posts.
See link #3.
December 18, 2020 at 1:21 pm #1586462TomLead DeveloperLead DeveloperLet’s tackle the columns issue first. Try this:
add_filter( 'generate_blog_columns', function( $columns ) { if ( ! is_singular() && 'your-cpt' === get_post_type() ) { return true; } return $columns; } );
Does that provide consistent results?
Let us know π
December 18, 2020 at 1:24 pm #1586464Goran DragutinovicYes that fixes the issue of the column.
December 19, 2020 at 12:25 pm #1587420TomLead DeveloperLead DeveloperAwesome. So what’s left to figure out? You’re wanting to add post meta to these cpt items as well?
Let me know π
December 19, 2020 at 12:28 pm #1587428Goran DragutinovicYes, the post meta is acting strangely. For example
When the CPT is the latest post in the loop, then all the meta gets stripped from all posts in that archive. Example: https://theeldergeek.com/movies/
But when it is not the latest item in the loop, the meta does not get stripped.
Example: https://theeldergeek.com/shows/December 19, 2020 at 11:39 pm #1587735Goran DragutinovicAlso:
– on category and tag archives, the thumbnail from entry from the custom post is showing up as around 1.5x bigger than others
– additionally, the custom post type category ‘geek-bios’ show non-custom post types as well. Is there a way to fix this to show only custom post types without creating a separate custom post type archive page?December 20, 2020 at 11:05 am #1588399TomLead DeveloperLead DeveloperYou will likely need to enable post meta on your CPTs:
https://docs.generatepress.com/article/generate_entry_meta_post_types/
https://docs.generatepress.com/article/generate-footer-meta-post-types/As for the thumbnail – where are you defining the size?
For the CPT archive – it will only show posts from its CPT if it’s set up correctly. If that’s not the case, perhaps you’re using a function somewhere altering the query?
December 21, 2020 at 10:12 am #1589697Goran DragutinovicYou will likely need to enable post meta on your CPT. – Worked
As for the thumbnail β where are you defining the size? Nowhere. Assumed it would inherit.
For the CPT archive. – two relevant functions are at the top of this thread. I will comb through the files again to see if I changed/added anything else anywhere,
December 21, 2020 at 11:29 am #1589774TomLead DeveloperLead DeveloperIt should inherit the image size from Customize > Layout > Blog – what do you have set up as your image values in there?
Let us know π
December 21, 2020 at 12:17 pm #1589819Goran DragutinovicWell, that helped me realize that as usual, it was my fault.
The test post feat img was of a diff size than the others. Thanks!Everything is solved now except for the CPT Archive.
Basically, it’s being treated as All Post and not isolating only my CPT published posts.
I’ve combed through the files and can’t see anything that I’ve changed anywhere pertinent.
December 22, 2020 at 11:00 am #1591058TomLead DeveloperLead DeveloperThe CPT archive should only contain posts from that CPT by default – that’s just how WordPress works. Perhaps one of your plugins is interfering with the query?
December 22, 2020 at 1:31 pm #1591215Goran DragutinovicHi Tom,
Is that the case even if I don’t have a separate archive for the CPTs and/or don’t have a separate taxonomy setup for them either (currently just have ‘taxonomies’ => array(‘category’, ‘post_tag’)?
December 22, 2020 at 2:13 pm #1591247Goran DragutinovicHey Tom, more info I just came across.
//Hooks into the query to show custom post types into the category archive
function add_cpt_to_category_archive( $query ) { if ( ! is_admin() && $query->is_main_query() ) { if ( is_archive() || is_category() || is_home() ) { // Add CPT to the category $query->set( 'post_type', array( 'post', 'geek-bios' ) ); } if ( $query->is_search() ) { // Add CPT to the search $query->set( 'post_type', array( 'post', 'geek-bios' ) ); } } } add_action('pre_get_posts', 'add_cpt_to_category_archive' );
I removed is_archive() from the above 2nd if statement and it seems to have done the trick.
Also removed the 3rd if statement as it is showing up in search even without that code.
Thoughts?
-
AuthorPosts
- You must be logged in to reply to this topic.