Skip Navigation

[Resolved] Load specific custom post types on archive pages with ajax pagination

This support ticket is created 2 years, 1 month ago. 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.

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 4 replies, has 3 voices.

Last updated by umbertoZ 2 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#2529695

Hi, in a previous topic Waqar gave me a solution to select CPTs to display on archive pages:

https://toolset.com/forums/topic/split-view-with-multiple-taxonomies-filter/

This is my custom code:

add_action( 'pre_get_posts', 'exclude_cpt' );
function exclude_cpt( $query ) {  

  if ( ($query->is_archive('region') && $query->is_main_query() && ! is_admin()) OR ($query->is_archive('discover') && $query->is_main_query() && ! is_admin()) OR ($query->is_archive('area') && $query->is_main_query() && ! is_admin()) OR ($query->is_archive('destination') && $query->is_main_query() && ! is_admin()) OR ($query->is_archive('counties') && $query->is_main_query() && ! is_admin()) OR ($query->is_archive('towns') && $query->is_main_query() && ! is_admin())  ) {
    
    $query->set( 'post_type', array('sight') );
  }
  return $query;
}

It works fine, but it doesn't if I activate AJAX pagination. On the first archive load it is ok, but if I click on the "next" link it loads also the not required CPTs.

How can I fix it?

cheers

#2530203

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

That's a pretty convoluted if statement, I didn't look at it too closely, but I note that it includes checks for !is_admin.

Not that is_admin() returns true for ajax requests from the front end.

You can also check whether DOING_AJAX is set and is true to differentiate back-end admin page requests from front-end ajax requests.

#2530231

Hi Nigel, can you show me an example of how I can use DOING_AJAX?

Thnak you
cheers

#2530241

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Basically you can check the ajax action with for example:

if( (defined( 'DOING_AJAX' ) and DOING_AJAX) or  wp_doing_ajax() ) {
 // ajax conditions satisfied 
}

Can you please check if that helps otherwise please send me admin access details and problem URL.

#2531735

My issue is resolved now. Thank you!