Skip Navigation

[Resuelto] How to use Toolset taxonomy as filter in admin

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I would like to filter the list of Products by a custom taxonomy in wp-admin.

Solution: Add the following custom code to functions.php:

function filter_admin_lists_by_taxonomies( $post_type, $which ) {
  // Array of post type slugs where these filters should appear
  $filter_types = ['product'];
  // Array of taxonomy slugs to filter by
  $taxonomies = ['your-taxonomy-slug'];
 
  if ( !in_array( $post_type, $filter_types ) )
    return;
 
  foreach ( $taxonomies as $taxonomy_slug ) {
 
    // Retrieve taxonomy data
    $taxonomy_obj = get_taxonomy( $taxonomy_slug );
    $taxonomy_name = $taxonomy_obj->labels->name;
 
    // Retrieve taxonomy terms
    $terms = get_terms( $taxonomy_slug );
 
    // Display filter HTML
    echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
    echo '<option value="">' . sprintf( esc_html__( 'Show All %s', 'text_domain' ), $taxonomy_name ) . '</option>';
    foreach ( $terms as $term ) {
      printf(
        '<option value="%1$s" %2$s>%3$s (%4$s)</option>',
        $term->slug,
        ( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
        $term->name,
        $term->count
      );
    }
    echo '</select>';
  }
 
}
add_action( 'restrict_manage_posts', 'filter_admin_lists_by_taxonomies' , 10, 2);

Modify the $filter_types and $taxonomies arrays as needed.

Relevant Documentation:
https://developer.wordpress.org/reference/hooks/restrict_manage_posts/

This support ticket is created hace 6 años, 10 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Etiquetado: 

Este tema contiene 2 respuestas, tiene 2 mensajes.

Última actualización por Pat hace 6 años, 9 meses.

Asistido por: Christian Cox.

Autor
Mensajes
#611766

Pat

Hello,

I have created a taxonomy related to products. I have been able to display all info related to this taxonomy in the product listing admin page. Now, I would like to be able to sort products in the admin product listing page thanks to this taxonomy (as it is possible with prodcut catogories for example), but this is not possible.
Is there something to do in order to get this feature avalable in the admin product listing page?

Regards
Pat

#612024

Pat I don't think it's possible to sort by a category, but you can filter by a category. Add the following code to functions.php:

function filter_admin_lists_by_taxonomies( $post_type, $which ) {
  // Array of post type slugs where these filters should appear
  $filter_types = ['product'];
  // Array of taxonomy slugs to filter by
  $taxonomies = ['your-taxonomy-slug'];

  if ( !in_array( $post_type, $filter_types ) )
    return;

  foreach ( $taxonomies as $taxonomy_slug ) {

    // Retrieve taxonomy data
    $taxonomy_obj = get_taxonomy( $taxonomy_slug );
    $taxonomy_name = $taxonomy_obj->labels->name;

    // Retrieve taxonomy terms
    $terms = get_terms( $taxonomy_slug );

    // Display filter HTML
    echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
    echo '<option value="">' . sprintf( esc_html__( 'Show All %s', 'text_domain' ), $taxonomy_name ) . '</option>';
    foreach ( $terms as $term ) {
      printf(
        '<option value="%1$s" %2$s>%3$s (%4$s)</option>',
        $term->slug,
        ( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
        $term->name,
        $term->count
      );
    }
    echo '</select>';
  }

}
add_action( 'restrict_manage_posts', 'filter_admin_lists_by_taxonomies' , 10, 2);

Replace 'your-taxonomy-slug' with the slug of your custom taxonomy.

#612071

Pat

Hello Christian,

That's perfect. Many thanks
Pat