[Resolved] Including more than one post type

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Including more than one post type

Tagged: 

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #13208
    Michael
    Participant

    Hi 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.

    #13241
    Tom
    Keymaster

    Hi 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/286caf1164db8b6b6f38b85d3a011b519a27f4de

    Then 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.

    #13336
    Michael
    Participant

    Many thanks Tom!

    #13350
    Tom
    Keymaster

    No problem! 🙂

    #23357
    Matt
    Participant

    Hi 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,
    Matt

    #23360
    Matt
    Participant

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

    #23527
    Tom
    Keymaster

    Hi there,

    You need to use the latest development version: https://wpshowposts.com/wp-show-posts-1-2-0/

    Let me know 🙂

    #23572
    Matt
    Participant

    Hi Tom,

    That works well now, thanks 🙂

    Matt

    #23659
    Tom
    Keymaster

    No problem! 🙂

    #28085
    Joon
    Participant

    Hello,

    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?

    #28118
    elvin
    Moderator

    Hi 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.

    #28128
    Joon
    Participant

    Ok, 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??

    #28135
    elvin
    Moderator

    Try 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 );
    #28139
    Joon
    Participant

    Great!! 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?

    • This reply was modified 1 month, 3 weeks ago by Joon.
    #28144
    elvin
    Moderator

    Check the codex for parameters:
    https://developer.wordpress.org/reference/classes/wp_query/#tag-parameters

    You can use tag or tag__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 );
Viewing 15 posts - 1 through 15 (of 21 total)
  • You must be logged in to reply to this topic.