Skip Navigation

[Resuelto] How to display only one custom post type in taxonomy archive ?

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:
How can I limit the output of an Archive for a taxonomy to only show a certain Post Type, if the queried Taxonomy is actually assigned to more than one Post type?

Solution:
1. Create a new View WordPress Archive for the taxonomy "your-tax"

2. In the Views Loop output whatever you want, conditionally:
As example, this below will output only Posts of Type "Post", in any term of the Taxonomy "Category":

[wpv-conditional if="( '[wpv-post-type]' eq 'post' )"][wpv-post-title][/wpv-conditional]

3. You do set up the conditional using the Conditional Output GUI.

Relevant Documentation:
https://toolset.com/documentation/user-guides/normal-vs-archive-views/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created hace 8 años. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 respuestas, has 2 mensajes.

Last updated by tyeB hace 8 años.

Assisted by: Beda.

Autor
Mensajes
#381129

Following up from this thread: https://toolset.com/forums/topic/remove-cpt-from-categorycategory-slug-page/

This issue still still exists using WP default 2016 theme and all plugins disabled except for WP-types and as this is used to create the CPT

#381203

Yes, this is because that code is 99% wrong

Please try:

//exclude Staff from Services loop
add_action( 'pre_get_posts', 'exclude_cpt' );

function exclude_cpt( $query ) {
    if ( $query->is_tax('service') ) { //Change the Tax here!!
        $query->set( 'post_type', array('project') ); //Change the Post type here!!
    }
    return $query;
}

Works?

#381274

Hey Beda - thanks for the reply.

This problem lies across all my categories - so I have used this code:

if ( $query->is_category() ) {

I've also tried is_archive() and is_tax() but still no luck.

If I reverse my code and only request the CPT to be listed - it works and only the cpt is displayed.

add_action( 'pre_get_posts', 'exclude_cpt' );
function exclude_cpt( $query ) {
    if ( $query->is_category() ) {
		if( ! is_admin() && $query->is_main_query() ) {
			$query->set('post_type',array('cpt'));
		}
    return $query;
}
}
#381700

Well, since this is Custom PHP Code, and the goal can be easily achieved with Toolset, I can not debug or suggest to go ahead with this Custom PHP Code which would need to be debugged.

Instead, I must suggest Toolset featured approaches as per our Support policy.

1. Create a new View WordPress Archive for the taxonomy "Categories"

2. In the Views Loop output whatever you want, conditionally:
As example, this below will output only Posts of Type "Post", in any term of the Taxonomy "Category":

 [wpv-conditional if="( '[wpv-post-type]' eq 'post' )"][wpv-post-title][/wpv-conditional]

3. This works great, I tested it locally and is the way we suggest to do it, since Toolset is a software to create websites with no PHP Code.
I set up the conditional using the Conditional Output GUI.

This is the DOC for this:
https://toolset.com/documentation/user-guides/normal-vs-archive-views/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

Thank you

#381799

Ah... perfect - thanks Beda... I think I set off on the wrong foot with all the posts about custom PHP code - I didn't even think to look at toolset 🙂

Thanks again

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.