Skip to main content
deleted 410 characters in body
Source Link
jessica
  • 1.4k
  • 12
  • 17

I wrote a function that allows you to pass post_type in the $args array to the get_terms() function. It also checks whether post_type was passed via $_GET (this prevents taxonomies that are shared between CPTs but aren't assigned to the current custom post type from being displayed... lame partial workaround for post count is wrong when using same taxonomy for 2 different CPT).:

HT to @braydon for writing the SQL.

 /**
 * terms_clauses
 *
 * filter the terms clauses
 *
 * @param $clauses array
 * @param $taxonomy string
 * @param $args array
 * @return array
**/
function terms_clauses($clauses, $taxonomy, $args)
{
    global $wpdb;
    $post_type = $args['post_type'] 
        ? $args['post_type']
        : $_GET['post_type'] && is_admin()
            ? $_GET['post_type']
            : false;
            
    if ($post_type$args['post_type'])
    {
        $clauses['join'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
        $clauses['where'] .= " AND p.post_type='$post_type'";  post_type='{$args['post_type']}'"; 
    }
    return $clauses;
}
add_filter('terms_clauses', 'terms_clauses', 10, 3);

I wrote a function that allows you to pass post_type in the $args array to the get_terms() function. It also checks whether post_type was passed via $_GET (this prevents taxonomies that are shared between CPTs but aren't assigned to the current custom post type from being displayed... lame partial workaround for post count is wrong when using same taxonomy for 2 different CPT).

HT to @braydon for writing the SQL.

 /**
 * terms_clauses
 *
 * filter the terms clauses
 *
 * @param $clauses array
 * @param $taxonomy string
 * @param $args array
 * @return array
**/
function terms_clauses($clauses, $taxonomy, $args)
{
    global $wpdb;
    $post_type = $args['post_type'] 
        ? $args['post_type']
        : $_GET['post_type'] && is_admin()
            ? $_GET['post_type']
            : false;
            
    if ($post_type)
    {
        $clauses['join'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
        $clauses['where'] .= " AND p.post_type='$post_type'";   
    }
    return $clauses;
}
add_filter('terms_clauses', 'terms_clauses', 10, 3);

I wrote a function that allows you to pass post_type in the $args array to the get_terms() function:

HT to @braydon for writing the SQL.

 /**
 * terms_clauses
 *
 * filter the terms clauses
 *
 * @param $clauses array
 * @param $taxonomy string
 * @param $args array
 * @return array
**/
function terms_clauses($clauses, $taxonomy, $args)
{
    global $wpdb;
            
    if ($args['post_type'])
    {
        $clauses['join'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
        $clauses['where'] .= " AND p.post_type='{$args['post_type']}'"; 
    }
    return $clauses;
}
add_filter('terms_clauses', 'terms_clauses', 10, 3);
added 35 characters in body
Source Link
jessica
  • 1.4k
  • 12
  • 17

I wrote a function that allows you to pass post_type in the $args array to the get_terms() function. It also checks whether post_type was passed via $_GET (this fixes the taxonomy counts in the wp admin pages forprevents taxonomies that are shared between more than oneCPTs but aren't assigned to the current custom post type -- seefrom being displayed... lame partial workaround for post count is wrong when using same taxonomy for 2 different CPT).

HT to @braydon for writing the SQL.

 /**
 * terms_clauses
 *
 * filter the terms clauses
 *
 * @param $clauses array
 * @param $taxonomy string
 * @param $args array
 * @return array
**/
function terms_clauses($clauses, $taxonomy, $args)
{
    global $wpdb;
    $post_type = $args['post_type'] 
        ? $args['post_type']
        : $_GET['post_type'] && is_admin()
            ? $_GET['post_type']
            : false;
            
    if ($post_type)
    {
        $clauses['join'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
        $clauses['where'] .= " AND p.post_type='$post_type'";   
    }
    return $clauses;
}
add_filter('terms_clauses', 'terms_clauses', 10, 3);

I wrote a function that allows you to pass post_type in the $args array to the get_terms() function. It also checks whether post_type was passed via $_GET (this fixes the taxonomy counts in the wp admin pages for taxonomies that are shared between more than one post type -- see post count is wrong when using same taxonomy for 2 different CPT).

HT to @braydon for writing the SQL.

 /**
 * terms_clauses
 *
 * filter the terms clauses
 *
 * @param $clauses array
 * @param $taxonomy string
 * @param $args array
 * @return array
**/
function terms_clauses($clauses, $taxonomy, $args)
{
    global $wpdb;
    $post_type = $args['post_type'] 
        ? $args['post_type']
        : $_GET['post_type'] && is_admin()
            ? $_GET['post_type']
            : false;
            
    if ($post_type)
    {
        $clauses['join'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
        $clauses['where'] .= " AND p.post_type='$post_type'";   
    }
    return $clauses;
}
add_filter('terms_clauses', 'terms_clauses', 10, 3);

I wrote a function that allows you to pass post_type in the $args array to the get_terms() function. It also checks whether post_type was passed via $_GET (this prevents taxonomies that are shared between CPTs but aren't assigned to the current custom post type from being displayed... lame partial workaround for post count is wrong when using same taxonomy for 2 different CPT).

HT to @braydon for writing the SQL.

 /**
 * terms_clauses
 *
 * filter the terms clauses
 *
 * @param $clauses array
 * @param $taxonomy string
 * @param $args array
 * @return array
**/
function terms_clauses($clauses, $taxonomy, $args)
{
    global $wpdb;
    $post_type = $args['post_type'] 
        ? $args['post_type']
        : $_GET['post_type'] && is_admin()
            ? $_GET['post_type']
            : false;
            
    if ($post_type)
    {
        $clauses['join'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
        $clauses['where'] .= " AND p.post_type='$post_type'";   
    }
    return $clauses;
}
add_filter('terms_clauses', 'terms_clauses', 10, 3);
Source Link
jessica
  • 1.4k
  • 12
  • 17

I wrote a function that allows you to pass post_type in the $args array to the get_terms() function. It also checks whether post_type was passed via $_GET (this fixes the taxonomy counts in the wp admin pages for taxonomies that are shared between more than one post type -- see post count is wrong when using same taxonomy for 2 different CPT).

HT to @braydon for writing the SQL.

 /**
 * terms_clauses
 *
 * filter the terms clauses
 *
 * @param $clauses array
 * @param $taxonomy string
 * @param $args array
 * @return array
**/
function terms_clauses($clauses, $taxonomy, $args)
{
    global $wpdb;
    $post_type = $args['post_type'] 
        ? $args['post_type']
        : $_GET['post_type'] && is_admin()
            ? $_GET['post_type']
            : false;
            
    if ($post_type)
    {
        $clauses['join'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
        $clauses['where'] .= " AND p.post_type='$post_type'";   
    }
    return $clauses;
}
add_filter('terms_clauses', 'terms_clauses', 10, 3);