Home › Forums › Pro Support › Including more than one post type
Tagged: CPTs
- This topic has 20 replies, 5 voices, and was last updated 2 years, 8 months ago by
Joon.
-
AuthorPosts
-
February 5, 2020 at 2:43 pm #13208
Michael
ParticipantHi Tom and team
Is there a way to create one ‘show posts’ to include multiple post types – for example, ‘posts’ and a few cpts?
Was thinking of using the Flint Skin Dispatch front page as a bit of a template/inspiration (on the Generate Press site library), but wanted to know if there was a way to mix in some cpts to the display.
Many thanks for any help and ideas. Just renewed GP and an amazed at the value.
Cheers
Mike
Ps apologies if I’ve missed anything obvious.
February 6, 2020 at 6:16 pm #13241Tom
KeymasterHi Mike,
It’s possible, but it’ll take some tweaking.
Make this change in your
wp-show-posts.php
file: https://github.com/tomusborne/wp-show-posts/commit/286caf1164db8b6b6f38b85d3a011b519a27f4deThen you can do this:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 123 === $settings['list_id'] ) { $args['post_type'] = array( 'post', 'another-type', 'one-more' ); } return $args; }, 10, 2 );
You just need to update
123
with the ID of the list.February 11, 2020 at 3:25 am #13336Michael
ParticipantMany thanks Tom!
February 11, 2020 at 5:28 pm #13350Tom
KeymasterNo problem! 🙂
November 29, 2020 at 5:59 am #23357Matt
ParticipantHi Tom,
I’m trying to get posts and pages to display and I used the following code:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 93689 === $settings['list_id'] ) { $args['post_type'] = array( 'post', 'page' ); } return $args; }, 10, 2 );
I get this error in the logs:
Error: Too few arguments to function {closure}(), 1 passed in /public_html/wp-includes/class-wp-hook.php on line 287 and exactly 2 expected in /public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:1 Stack trace: #0 /public_html/wp-includes/class-wp-hook.php(287): {closure}() #1 /public_html/wp-includes/plugin.php(206): WP_Hook->apply_filters() #2 /public_html/wp-content/plugins/wp-show-posts/wp-show-posts.php(383): apply_filters() #3 /public_html/wp-content/themes/ckyf-divi/category-cours-kriya-yoga.php(8): wpsp_display()
93689 is my list ID.
The page blanks out where the shortcode should appear. If I remove the code snippet, the shortcode works (with posts only). Is there something else I should do?
Thanks,
MattNovember 29, 2020 at 6:32 am #23360Matt
ParticipantIt looks like $settings is never used: if I remove it from the function arguments and remove the number of arguments, it works but it means all shortcodes will have this condition ( posts and pages).
December 1, 2020 at 10:46 am #23527Tom
KeymasterHi there,
You need to use the latest development version: https://wpshowposts.com/wp-show-posts-1-2-0/
Let me know 🙂
December 1, 2020 at 4:18 pm #23572Matt
ParticipantHi Tom,
That works well now, thanks 🙂
Matt
December 2, 2020 at 12:34 pm #23659Tom
KeymasterNo problem! 🙂
March 16, 2021 at 1:36 am #28085Joon
ParticipantHello,
I am trying to achieve something similar. I am trying to get one list to show new posts + events from “The Events Calendar”. I already checked that they work fine in a list by themselves.
I just can’t get it working. I started by trying to make the PHP change you mentioned here: https://github.com/tomusborne/wp-show-posts/commit/286caf1164db8b6b6f38b85d3a011b519a27f4de
At this time I had WP Show Posts 1.1.3 active + WP Show Posts Pro 1.0.0. The I added the line to Code Snippets, and changed the list number + post type name.add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 16270 === $settings['list_id'] ) { $args['post_type'] = array( 'post', 'tribe_events' ); } $query = new WP_Query( apply_filters( 'wpsp_query_args', $args, $settings ) ); return $args; }, 10, 2 );
Nothing changed. The list still only showed posts?
Then I installed WPSP 1.2.0-alpha.3 and went back to wp-show-posts.php and searched for the query line. I found this:
// phpcs:ignore -- Filter kept for backward compatibility. $args = apply_filters( 'wp_show_posts_shortcode_args', $args, $settings );
So I guess no changes needed there because the settings parameter is already added? But still nothing. The list still only shows posts. Am I doing something wrong?
Also, will this code pull all posts from these two post types, or could you also still require a tag?
March 16, 2021 at 9:52 pm #28118elvin
ModeratorHi Joon,
WPSP 1.1.3 doesn’t have
wp_show_posts_shortcode_args
‘s $settings . The change was required so you could target a specific list_id with$settings['list_id']
.IF you’re using 1.2.0, you don’t have to do that modification as Tom already added it in.
As for the filter usage:
Also, will this code pull all posts from these two post types, or could you also still require a tag?
You don’t need to include this line:
$query = new WP_Query( apply_filters( 'wpsp_query_args', $args, $settings ) );
And yes it will pull posts from the 2 post_types specified. You don’t need to include a tag but you can if you want to specify the query even more.
March 16, 2021 at 11:35 pm #28128Joon
ParticipantOk, I just wanted to provide some background. Right now I am actually using 1.2.0.
But for some reason I can’t get it to work.
Here is the code I added to code snippets. The WP Show Posts list number “16270” is correct, I double-checked it. The post types I want to show in the list are posts, pages and one CPT called “hanke”.
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 16270 === $settings['list_id'] ) { $args['post_type'] = array( 'post', 'page', 'hanke' ); } return $args; }, 10, 2 );
The list still only shows posts… What can I be doing wrong??
March 17, 2021 at 12:12 am #28135elvin
ModeratorTry it like this:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if(16270 === (int)$settings['list_id']){ $args['post_type'] = array('post','page','hanke'); } return $args; }, 15, 2 );
March 17, 2021 at 12:16 am #28139Joon
ParticipantGreat!! Thank you so much. That fixed it. 🙂
If I would like to include a tag to specify the query even more, how would I go about it?
March 17, 2021 at 12:25 am #28144elvin
ModeratorCheck the codex for parameters:
https://developer.wordpress.org/reference/classes/wp_query/#tag-parametersYou can use
tag
ortag__in
depending on specificity.Example:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if(16270 === (int)$settings['list_id']){ $args['post_type'] = array('post','page','hanke'); $args['tag'] = 'cats'; } return $args; }, 15, 2 );
-
AuthorPosts
- You must be logged in to reply to this topic.