Support » Plugin: Custom Post Type UI » Portfolio items do not display

  • bergmans23

    (@bergmans23)


    Hi there,
    I’m having trouble with some CPT UI custom code that removes s category from my slugs.
    This, it does fine, but it also makes the portfolio items not being displayed.

    Things go wrong with the second function.

    function cptui_demo_remove_slug_from_permalink_string( $post_link, $post ) {
    
    	if ( 'publish' !== $post->post_status ) {
    		return $post_link;
    	}
    
    	if ( 'diensten' === $post->post_type ) {
    		$post_link = str_replace(
    			'/' . $post->post_type . '/',
    			'/',
    			$post_link
    		);
    	}
    
    	return $post_link;
    }
    add_filter( 'post_type_link', 'cptui_demo_remove_slug_from_permalink_string', 10, 2 );
    
    function cptui_demo_add_post_type_to_query( $query ) {
    
    	if ( is_admin() || ! $query->is_main_query() ) {
    		return;
    	}
    
    	if ( empty( $query->query['name'] ) ) {
    		return;
    	}
    
    	$query->set(
    		'post_type',
    		[
    			'post',
    			'page',
    			'diensten',
    		]
    	);
    }
    add_action( 'pre_get_posts', 'cptui_demo_add_post_type_to_query' );

    Hope you can help!
    Thanks
    Bye
    Ward

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can you provide some example permalinks for the ones failing?

    To be clear, this isn’t doing anything with categories, it is doing things with removing the post type slug from the permalinks. The second function is basically telling WP to consider the diensten post type with every query, in case the permalink is supposed to match up with a post, because it was taken out with the first function.

    Thread Starter bergmans23

    (@bergmans23)

    Hi Michael, hope you’re well?

    Yes you’re right, nothing to do with categories, but because the old website had no “diensten” in the slug, I had to loose that in the slug of the new site. That’s why the first function.

    But, to be able to call the “diensten” through menu’s and grids, I added function two. This worked great for a while, but all of a sudden, the portfolio pages are not being displayed anymore, until I delete function two. But then, the “diensten” are not being called.

    You can find them in two grids on the page:

    https://www.vangastelverhuizingen.nl/verhuur-en-verkoop-materiaal/

    I’m talking about the rental items like:

    https://www.vangastelverhuizingen.nl/portfolio/verhuisdozen-verhuur/
    https://www.vangastelverhuizingen.nl/portfolio/kunststof-rolbox/
    https://www.vangastelverhuizingen.nl/portfolio/verhuisdeken/

    and so on.

    Trouble is, when I delete the second function, pages like https://www.vangastelverhuizingen.nl/verhuur-en-verkoop-materiaal/
    https://www.vangastelverhuizingen.nl/particuliere-verhuizingen/
    and so on are not being called through the menu’s.

    Hope you can help!
    Bye
    Ward

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Is portfolio a different post type? Or a category? or something else?

    Honestly the biggest question really is what is the query being made for those portfolio items, and that’s something that I can’t debug from here. Logically the code in the second function doesn’t look like it’d be affecting those portfolio urls at all, unless it’s taking out the post type in question and it needs to be more inclusive.

    Perhaps give this a try for some of the code in the second function:

    $cptui_post_types = cptui_get_post_type_slugs();
    
    $query->set(
    	'post_type',
    	array_merge(
    		array( 'post', 'page' ),
    		$cptui_post_types
    	)
    );
    

    This will grab all the post types with CPTUI, plus “post” and “page” and make them all considered.

    Thread Starter bergmans23

    (@bergmans23)

    Hi Michael,

    Yep, Portfolio is a different post type, native to Impreza theme. You’re plugin is one of their all time advised addons!
    My second function looks like this now:

    function cptui_demo_add_post_type_to_query( $query ) {
    
    	if ( is_admin() || ! $query->is_main_query() ) {
    		return;
    	}
    
    	if ( empty( $query->query['name'] ) ) {
    		return;
    	}
    	$cptui_post_types = cptui_get_post_type_slugs();
    
    	$query->set(
    		'post_type',
    		array_merge(
    			array( 'post', 'page' ),
    			$cptui_post_types
    		)
    	);
    }
    add_action( 'pre_get_posts', 'cptui_demo_add_post_type_to_query' );

    But the portfolio items still don’s show.
    Bye
    Ward

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Likely due to the fact that the portfolio post type is coming from the theme, and not CPTUI. My suggested edits above was with the assumption that it was registered with CPTUI as well.

    So,

    You could either keep what you have now and add in “portfolio” like so:

    $query->set(
    	'post_type',
    	array_merge(
    		array( 'post', 'page', 'portfolio' ),
    		$cptui_post_types
    	)
    );
    

    Or you could go back to what you had and just amend like so:

    $query->set(
    	'post_type',
    	[
    		'post',
    		'page',
    		'diensten',
    		'portfolio',
    	]
    );
    
    Thread Starter bergmans23

    (@bergmans23)

    Hi Michael,

    Thanks! Problem is, neither of them work.
    I tried option two before I contacted you, thinking that would be the most logical solution, but no…

    Bye
    Ward

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’m out of ideas at this point, truth be told. Something is definitely going on with some queries and it’s impossible for me to say what right now, with my suggestions above failing so far.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Did you ever get this one resolved @bergmans23 ?

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.