WordPress post_where & posts_join not working only for custom post type

I have a custom WordPress plugin built for booking courses etc.

My custom plugin has some new tables in which custom data like slot date, time etc are stored.

I am trying to modify archive query for courses so it is ordered by slot date from another table so i am using posts_join hook to LEFT JOIN my courses table with posts table and then posts_orderby to order results by slot_date

I want to run this only on my courses archive page but it seems to be running everywhere across the site.

So i used an if statement witin posts_join hook to filter by post type but the site gives a critical error saying Memory exhausted.

Am i doing something wrong?

add_filter( 'posts_join', 'add_other_table', 10, 2 );

function add_other_table( $join, $wp_query ) {
if ($wp_query-query_vars['post_type'] == 'courses') {
 global $wpdb;
$join .=  INNER JOIN {$wpdb-prefix}course_slots as courseslots on courseslots.post_id = $wpdb-posts.ID;
 return $join;
}
}

add_filter( 'posts_where', 'ss_where_table', 10, 2 );

function ss_where_table( $where, $wp_query ) {
if ($wp_query-query_vars['post_type'] == 'courses') {
 global $wpdb;
 $where .= $wpdb-prepare(  AND courseslots.post_id=$wpdb-posts.ID );
 return $where;
}
}

Topic join-tables custom-post-type-archives plugin-development custom-post-types Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.