Skip Navigation

[Resolved] Category archive, excluding the referrer CPT

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 1 voice.

Last updated by onwebdev 11 hours, 31 minutes ago.

Assisted by: Minesh.

Author
Posts
#2818515

I have a CPT ( Hotels) .

It fals under several categories ( example - featured) . I was trying to create a category archive, that excludes the referrer hotel from the list being displayed.
Is it possible?
Thanks.

#2818526

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you mean that you want to exclude the posts where category is set to featured?

#2818536

Hi Minesh, hope you are doing well.

I wanted the referrer single CPT to be excluded from the category archive.

For example - single hotel with the category link - reader clicks the link - goes to the category archive - here the referrer hotel gets excluded.

Thanks.

#2818566

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - you mean you want to exclude the post from the taxonomy/category archvie from where user clicked on the archive link. Lets say you are on post A and you click on the taxonomy/category archive link on that post and now on taxonomy/category archvie you want to exlcude post A - correct?

#2818587

Yes, absolutely right

#2818877

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you should try to use the native WordPress pre_get_post hook in order to exclude the referrer post.

For example:

function exclude_referrer_post_from_tax_archive( $query ) {
    // Only modify the main query on taxonomy archives
    if ( !is_admin() && $query->is_main_query() && is_tax() ) {

        // Get the referring URL
        $referer = wp_get_referer();

        if ( $referer ) {
            // Try to extract the post ID from the referer URL
            $referer_post_id = url_to_postid( $referer );

            if ( $referer_post_id ) {
                // Exclude the referring post from the query
                $query->set( 'post__not_in', array( $referer_post_id ) );
            }
        }
    }
}
add_action( 'pre_get_posts', 'exclude_referrer_post_from_tax_archive',99,1);

- you can adjust the code as required for your taxonomy archive.

#2819552

Thanks Minesh.

Big help, thank you.