Terms not shown for custom post type
-
I am displaying a custom post type and I am also including the terms so I can have the category below the post. The terms are not visible, though. It’s a custom post type created from a plugin. The markup is displayed in the code but it’s empty:
<div class="wp-show-posts-entry-meta wp-show-posts-entry-meta-below-post post-meta-stack"><span class="wp-show-posts-terms wp-show-posts-meta"></span></div>Is there a reason this is happening?
-
Hi George,
Not exactly sure why it’s occurring.
It’s likely how the CPT is set up.
I’ve made a quick demo for you here – https://dev-generate-press.pantheonsite.io/swords-wpsp/ – which shows “swords” cpt. You can see that there’s a title and date displaying which means its not empty.
Are you using a plugin for the CPT creation? You may want to consider asking its support or referring to its documentation on how to set things up properly. 😀
Maybe I wasn’t clear enough. I don’t have a problem with the date and author meta but only with the category.
As a test, I created a custom post type on a clean WP install with the CPT UI plugin, named “Events” and also a taxonomy called “Events category”. I created a test Event post and a test category to which I assigned this test post. Then I created a WP Show Post list set it to grab posts from the “Events” custom post type and set it to “Include terms” but the term is not visible in the front end. When I set the list to grab posts from the default WP post type, the term is visible in the front end.
Hope it’s clear now.
When you open the CPT’s default archive page, does the term meta appear?
WPSP’s code for the terms is pretty straightforward. It gets whatever is associated with the post, it’s here – https://github.com/tomusborne/wp-show-posts/blob/35e410d7800273fc66f211c0f80d553e95d17f83/inc/functions.php#L103-L105
No, they don’t, I saw this article, it works for header meta:
https://docs.generatepress.com/article/generate_entry_meta_post_types/But the article for footer meta (where the category is also included), doesn’t work:
https://docs.generatepress.com/article/generate-footer-meta-post-types/Try adding this PHP snippet:
add_filter( 'wpsp_terms_output', function( $output, $settings ){ $output = sprintf( '<span class="wp-show-posts-terms wp-show-posts-meta">%1$s</span>', get_the_term_list( get_the_ID(), $settings[ 'taxonomy' ], '', apply_filters( 'wpsp_term_separator', ', ' ) ) ); return $output; },15,2);Let us know how it goes.
It throws an error:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function {closure}(), 1 passed in SITEDIR\wp-includes\class-wp-hook.php on line 292 and exactly 2 expected in SITEDIR\wp-content\themes\themename\functions.php:83 Stack trace: #0 SITEDIR\wp-includes\class-wp-hook.php(292): {closure}('<span class="wp...') #1 SITEDIR\wp-includes\plugin.php(212): WP_Hook->apply_filters('<span class="wp...', Array) #2 SITEDIR\wp-content\plugins\wp-show-posts\inc\functions.php(104): apply_filters('wpsp_terms_outp...', '<span class="wp...') #3 SITEDIR\wp-content\plugins\wp-show-posts\inc\functions.php(143): wpsp_meta('below-post', Array) #4 SITEDIR\wp-includes\class-wp-hook.php(292): wpsp_add_post_meta_after_content(Array) #5 SITEDIR\wp-includes\class-wp-hook.php(316): WP_Hook->apply_filters(NULL, Ar in SITEDIR\wp-content\themes\themename\functions.php on line 83I code my snippets straight to my child theme’s functions.php. Even if I remove all other snippets (they are not a lot) this code still created the error. In fact, it also throws a critical error on the page’s backend where the shortcode is present as well.
You sure, you are sending me the right code?
I code my snippets straight to my child theme’s functions.php. Even if I remove all other snippets (they are not a lot) this code still created the error. In fact, it also throws a critical error on the page’s backend where the shortcode is present as well.
You sure, you are sending me the right code?
I test on my sandbox site before posting and it works.
Can you check if there are multiple usages of the same filter?
Normally, we don’t need to use this on any CPT as it should be working as it is. But it can get messed up if the filter is already in use prior to me suggesting it. 😀
Say, for example, you’ve used the same filter to display only the tag on one of the WPSP lists –
add_filter( 'wpsp_terms_output', function( $output ){ $output = sprintf( '<span class="wp-show-posts-terms wp-show-posts-meta">%1$s</span>', get_the_term_list( get_the_ID(), 'post_tag', '', apply_filters( 'wpsp_term_separator', ', ' ) ) ); return $output; });This can be an issue because this applies to all WPSP lists.
While this will work for WPSP lists of default post query with posts that have tags, it won’t display any terms on WPSP lists with CPT query without any post tags.
Can you check if this is the case?
We can help you check things out in the backend if want. You can send the details through our official contact page – https://wpshowposts.com/contact/
Let us know. 😀
Do you also include this code in your example?
https://docs.generatepress.com/article/generate-footer-meta-post-types/From the GP documentation, this needs to be there as well, it shouldn’t display meta for custom post types without it.
-
This reply was modified 4 years, 11 months ago by
George.
Do you also include this code in your example?
https://docs.generatepress.com/article/generate-footer-meta-post-types/From the GP documentation, this needs to be there as well, it shouldn’t display meta for custom post types without it.
That won’t be needed. WPSP is independent from GP Premium settings.
That particular code is so the GP premium customizer settings apply on CPTs as well.
WPSP doesn’t need that as it’s query is pretty straightforward. It gets the post and taxonomy (get_the_term_list) from the WPSP settings on the Post tab and displays whatever it gets.
It’s basically what you see here – https://developer.ww.wp.xz.cn/reference/classes/wp_query/#taxonomy-parameters – in UI.
You know what? It was my omission. Inside the WP Show Post list, I hadn’t selected the
events-categorytaxonomy under the Taxonomy section. For some reason, I thought, I didn’t need to. It displays the categories fine now. The only thing is I feel bad for wasting your time!I am sorry and thank you!
Glad you got it sorted now. No problem. 😀
-
This reply was modified 4 years, 11 months ago by
The topic ‘Terms not shown for custom post type’ is closed to new replies.