admin-taxonomy-filter icon indicating copy to clipboard operation
admin-taxonomy-filter copied to clipboard

Not working anymore since WP 5.5

Open unitb-ore opened this issue 5 years ago • 8 comments

The plugin worked fine in June with WP 5.4.2.

But now with WP 5.5.1 the post search is broken, so that no result are shown. Also the select box "All Schlagwörter" ("All tags") is empty ... and causes the browser to freeze a few seconds, when I try to open the select box.

Can you reproduce it with WP 5.5.x and can / will you provide a fix?

Otherwise I have to find another plugin / solution. :(

Thanks!

unitb-ore avatar Oct 08 '20 12:10 unitb-ore

@unitb-ore did you find a solution for this problem?

Hbitvof avatar Dec 05 '20 12:12 Hbitvof

No, I disabled this plugin (and have no other solution yet). :-(

unitb-ore avatar Dec 07 '20 09:12 unitb-ore

@unitb-ore I found a solution. Add this code to your functions.php:

function product_attribute_sorting_dropdown() {
    global $typenow;
    $taxonomy  = 'pa_brand';
    if ( $typenow == 'product' ) {
        $selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
        $info_taxonomy = get_taxonomy($taxonomy);
        
        wp_dropdown_categories(array(
            'show_option_all' => __("{$info_taxonomy->labels->name}"),
            'taxonomy'        => $taxonomy,
            'name'            => $taxonomy,
            'orderby'         => 'name',
            'selected'        => $selected,
            'show_count'      => true,
            'hide_empty'      => true,
        ));
    };
}

add_action('parse_query', 'product_attribute_sorting_query');
function product_attribute_sorting_query( $query ) {
    global $pagenow;
    $taxonomy  = 'pa_brand';
    $q_vars    = &$query->query_vars;

if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && isset($_GET[$taxonomy]) && is_numeric($_GET[$taxonomy]) && $_GET[$taxonomy] != 0) {
        
        $tax_query = (array) $query->get('tax_query');
        $term = get_term_by('id', $_GET[$taxonomy], $taxonomy);

        $tax_query[] = array(
               'taxonomy' => $taxonomy,
               'field' => 'slug',
               'terms' => array($term->slug), 
               'operator' => 'AND'
        );
    
        $query->set( 'tax_query', $tax_query );
    }

I used it for the custom pa_brand taxonomy. Guess you can give this a try?

Hbitvof avatar Dec 07 '20 09:12 Hbitvof

Thx!

How do you use "product_attribute_sorting_dropdown"?

unitb-ore avatar Dec 07 '20 10:12 unitb-ore

@unitb-ore I don't understand your question :). What do you want to know exactly?

Hbitvof avatar Dec 07 '20 10:12 Hbitvof

How can I integrate a select box for the taxonomy into the page "/wp-admin/edit.php"? When I call your function "product_attribute_sorting_dropdown" and rewrite the code for "post_tag", then a select box appears on this page showing all tags -- fine. But it is in the upper left corner and it is ignored for the search when clicking on "Filter" (button).

function product_attribute_sorting_dropdown() {
	global $pagenow;

	$paramName  = 'taxonomy';

	if ($pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'post') {
		$selected      = isset($_GET[$paramName]) ? $_GET[$paramName] : '';
		$info_taxonomy = get_taxonomy('post_tag');

		wp_dropdown_categories(array(
			'show_option_all' => __("{$info_taxonomy->labels->name}"),
			'taxonomy'        => 'post_tag',
			'name'            => $paramName,
			'orderby'         => 'name',
			'selected'        => $selected,
			'show_count'      => true,
			'hide_empty'      => true,
		));
	}
}

unitb-ore avatar Dec 07 '20 10:12 unitb-ore

Did you add the complete code? I have it integraded as a normal dropdown box:

image

Hbitvof avatar Dec 07 '20 10:12 Hbitvof

Okay, I found a solution: https://wordpress.stackexchange.com/questions/578/adding-a-taxonomy-filter-to-admin-list-for-a-custom-post-type

Now it works! :)

Here is my complete solution:

add_action('parse_query', 'filter_posts_by_tag');

function filter_posts_by_tag($query) {
	global $pagenow;

	$paramName   = 'taxonomy';
	$q_vars      = &$query->query_vars;
	$isPostsPage = ($pagenow == 'edit.php') && isset($q_vars['post_type']) && ($q_vars['post_type'] == 'post');
	$hasTagParam = isset($q_vars[$paramName]) && is_numeric($q_vars[$paramName]) && ($q_vars[$paramName] != 0);

	if ($isPostsPage && $hasTagParam) {
		$tax_query = (array) $query->get('tax_query');
		$term = get_term_by('id', $_GET[$paramName], 'post_tag');

		if ($term) {
			$tax_query[] = array(
				'taxonomy' => 'post_tag',
				'field' => 'slug',
				'terms' => array($term->slug),
				'operator' => 'AND'
			);

			$query->set('tax_query', $tax_query);
		}
	}
}

add_action('restrict_manage_posts', 'add_tag_filter_to_posts_admin_page');

function add_tag_filter_to_posts_admin_page() {
	global $typenow;
	global $wp_query;

	if ($typenow == 'post') {
		$paramName     = 'taxonomy';
		$query         = &$wp_query->query;
		$selected      = isset($query[$paramName]) ? $query[$paramName] : '';
		$info_taxonomy = get_taxonomy('post_tag');

		wp_dropdown_categories(array(
			'show_option_all' => __("Alle {$info_taxonomy->labels->name}"),
			'taxonomy'        => 'post_tag',
			'name'            => $paramName,
			'orderby'         => 'name',
			'selected'        => $selected,
			'hierarchical'    => false,
			'show_count'      => true,	// Show # posts
			'hide_empty'      => true,	// Don't show tags w/o posts
		));
	}
}

I do not understand, why it does not work, when I change "$paramName" to a different value (then the selected value is not set in the select box). But with this $paramName it works. 👍

unitb-ore avatar Dec 07 '20 11:12 unitb-ore