• Resolved hussein87

    (@hussein87)


    Hi,

    I’m trying to add multiple post types in one post list, I’ve already checked TOM’s PHP Snippet here
    https://wpshowposts.com/support/topic/including-more-than-one-post-type/#post-13241

    I’m using Version 1.1.3 and for some reason, the snippet broke the website when I added it to the child theme’s functions.php file.
    Error message: There has been a critical error on this website.

    The code I’m adding:

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        if ( 4491 === $settings['list_id'] ) {
            $args['post_type'] = array( 'post', 'tribe_events' );
        }
    
        return $args;
    }, 10, 2 );
    • This topic was modified 4 years, 5 months ago by hussein87.
    • This topic was modified 4 years, 5 months ago by hussein87.
    • This topic was modified 4 years, 5 months ago by hussein87.
    • This topic was modified 4 years, 5 months ago by hussein87.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Elvin

    (@ejcabquina)

    Hi there,

    Did you make the change on the plugin as Tom mentioned on the reply?

    It’s important for this snippet to work as the filter will have to be edited to take 2 args. Else, it will return an error. πŸ˜€

    Thread Starter hussein87

    (@hussein87)

    Thanks for the quick replay!

    Yes, I did and still getting the same error, it looks like this now

    	// Start the query
    	$query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args ) );
    	$query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args, $settings ) );

    What could be the mistake I’m making here?

    Plugin Support Elvin

    (@ejcabquina)

    Ah that’s the issue.

    It’s supposed to be just 1 line.

    It’s basically replacing this line:

    $query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args ) );

    with:

    $query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args, $settings ) );

    Thread Starter hussein87

    (@hussein87)

    Yes, that was the issue πŸ˜€
    Thank you so much for your help!

    Can I ask one last question please,
    I’m trying to add tow custom post type with specific categories from different post types, but it shows only posts from the first category which is “current-affairs” and doesn’t show the other post type category events.

    here’s the code I’m adding

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        if ( 4491 === $settings['list_id'] ) {
            $args['post_type'] = array( 'post', 'tribe_events' );
        $args['tax_query'] = array(
    			'relation' => 'OR',
    			array(
    				'taxonomy' => 'category',
    				'field'    => 'slug',
    				'terms'    => array( 'current-affairs','my-events' ),
    			),
        	);
        }
    	return $args;
    },10,2);
    Plugin Support Elvin

    (@ejcabquina)

    Is my-events under the same taxonomy as current-affairs?

    If the CPT has its own taxonomy with a term my-events, you’ll have another array

    array(
    				'taxonomy' => 'category',
    				'field'    => 'slug',
    				'terms'    => array( 'current-affairs','my-events' ),
    			),

    – to add another taxonomy and its term to the query.

    Like this

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        if ( 4491 === $settings['list_id'] ) {
            $args['post_type'] = array( 'post', 'tribe_events' );
        $args['tax_query'] = array(
    			'relation' => 'OR',
    			array(
    				'taxonomy' => 'category',
    				'field'    => 'slug',
    				'terms'    => array( 'current-affairs' ),
    			),
    			array(
    				'taxonomy' => 'your_cpt's_taxonomy_slug',
    				'field'    => 'slug',
    				'terms'    => array( 'my-events' ),
    			),
        	);
        }
    	return $args;
    },10,2);
    Thread Starter hussein87

    (@hussein87)

    Worked like a charm! πŸ˜€
    Yes, each one has its own taxonomy.

    Thanks again for your help.

    Plugin Support Elvin

    (@ejcabquina)

    No problem. πŸ˜€

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Multiple post types in one post list’ is closed to new replies.