Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working anymore since WP 5.5 #6

Open
unitb-ore opened this issue Oct 8, 2020 · 8 comments
Open

Not working anymore since WP 5.5 #6

unitb-ore opened this issue Oct 8, 2020 · 8 comments

Comments

@unitb-ore
Copy link

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!

@Hbitvof
Copy link

Hbitvof commented Dec 5, 2020

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

@unitb-ore
Copy link
Author

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

@Hbitvof
Copy link

Hbitvof commented Dec 7, 2020

@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?

@unitb-ore
Copy link
Author

Thx!

How do you use "product_attribute_sorting_dropdown"?

@Hbitvof
Copy link

Hbitvof commented Dec 7, 2020

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

@unitb-ore
Copy link
Author

unitb-ore commented Dec 7, 2020

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,
		));
	}
}

@Hbitvof
Copy link

Hbitvof commented Dec 7, 2020

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

image

@unitb-ore
Copy link
Author

unitb-ore commented Dec 7, 2020

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. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants