Rafael Merz
Rafael Merz

Reputation: 1

Filtering posts with custom taxonomies on Wordpress admin

I have a Wordpress site with several products. They're added as posts (just changed the label to products), and they have two custom taxonomies. I've added a dropdown for each of these custom taxonomies on the post admin screen using the following code:

add_action('restrict_manage_posts', 'product_type_filter');
function product_type_filter() {
  global $typenow;
  $post_type = 'post';
  $taxonomies = array('linha','aplicacoes');
  if ($typenow == $post_type) {
    foreach ( $taxonomies as $taxonomy ) {
      $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
      $info_taxonomy = get_taxonomy($taxonomy);
      wp_dropdown_categories(
        array(
          'show_option_all' => __("Todas as {$info_taxonomy->label}"),
          'taxonomy' => $taxonomy,
          'name' => $taxonomy,
          'orderby' => 'name',
          'selected' => $selected,
          'value_field' => 'slug',
          'show_count' => false,
          'hide_empty' => true,
        )
      );
    }
  };
}

That's creating and displaying the 2 dropdowns correctly (see screenshot attached). Problem is, when you select options of that dropdown and hit Filter to filter the products, the result is often wrong. I say often because for certain specific taxonomies the filter works, which is really bugging me. And yes, every single option displayed on the dropdown have products assigned to it.

wordpress admin post page screenshot

I managed to understand the issue by noticing that the url after the filter submit looks like this (when it works):

http://www.mysitedomain.com.br/wp-admin/edit.php?post_status=all&post_type=post&m=0&cat=0&lang=pt-br&linha=embare-cle-redonda&filter_action=Filtrar&paged=1

And like this (when it returns no posts despite the fact that there are posts to show matching the criteria):

http://www.mysitedomain.com.br/wp-admin/edit.php?s&post_status=all&post_type=post&action=-1&m=0&cat=0&lang=pt-br&linha=puruba&filter_action=Filtrar&paged=1&action2=-1

Notice this search (s) parameter added to the start of second url? /wp-admin/edit.php? s& post_status=all&post_type=post&action=-1&m=0&cat=0&lang=pt-br&linha=puruba&filter_action=Filtrar&paged=1&action2=-1

If I remove that parameter the correct posts are loaded. Anyone has any clues on how to solve that? I suppose it's a Wordpress core issue, but I have no idea why it only happens to certain terms of each taxonomy.

Btw I'm running Wordpress 4.8

Upvotes: 0

Views: 3388

Answers (1)

Tom
Tom

Reputation: 369

can you try adding this function after one you have?

function filterPosts($query) {

 global $pagenow;

 $qv =& $query->query_vars;

 if (
    $pagenow == 'edit.php' &&
    isset( $qv['tax-slux'] ) &&
    ctype_digit( $qv['tax-slug'] ) // stricter than is_numeric()
 ) {
    if ( $term = get_term_by( 'id', $qv['tax-slug'], 'tax-slug' ) ) {
        $qv['tax-slug'] = $term->slug;
    }
 }
}
add_filter('parse_query', 'filterPosts');

Edit:

Here's the full function that works for me and my custom post type

function filterSomePosts() {
  global $typenow;
  $post_type = 'post_type';
  $taxonomy  = 'some-taxonomy';
  if ($typenow == $post_type) {
    $selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
    $info_taxonomy = get_taxonomy($taxonomy);
    wp_dropdown_categories(array(
      'show_option_all' => __("Filter by some posts"),
      'taxonomy'        => $taxonomy,
      'name'            => $taxonomy,
      'orderby'         => 'name',
      'selected'        => $wp_query->query[$taxonomy],
      'show_count'      => true,
      'hide_empty'      => true,
    ));
  };
}
add_action('restrict_manage_posts', 'filterSomePosts');
function showFilteredResults($query) {

    global $pagenow;

    $qv =& $query->query_vars;

    if (
        $pagenow == 'edit.php' &&
        isset( $qv['some-taxonomy'] ) &&
        ctype_digit( $qv['some-taxonomy'] ) // stricter than is_numeric()
    ) {
        if ( $term = get_term_by( 'id', $qv['some-taxonomy'], 'some-taxonomy' ) ) {
            $qv['some-taxonomy'] = $term->slug;
        }
    }
}
add_filter('parse_query', 'showFilteredResults');

Can you give this function a go, update your post type and taxonomy slug.

Upvotes: 2

Related Questions