Support » Plugin: Code Snippets » snippet to sort post list by tags

  • Resolved electrolund

    (@electrolund)


    I’m trying to get this suggestion to work. It’s a nifty idea, to add tag filtering into the admin post list area.

    This half way works for me. The tag drop down is added, but then selecting one of my tags and hitting the Filter button doesn’t actually filter by the selection.

    Ideas?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    My apologies for taking so long to get back to you.

    I took a look at the WordPress StackExchange question you linked and cleaned up the code in the answer, while fixing what seems to be preventing it from working.

    Here’s the new code:

    add_action( 'restrict_manage_posts', function () {
    
    	if ( 'post' !== $GLOBALS['typenow'] ) {
    		return;
    	}
    
    	$tax_obj = get_taxonomy( 'post_tag' );
    	$terms = get_terms( 'post_tag' );
    
    	if ( ! $terms ) {
    		return;
    	}
    
    	echo '<select name="tag" id="tag" class="postform">';
    	echo '<option value="0">', esc_html( $tax_obj->labels->all_items ), '</option>';
    
    	$current = isset( $_GET['tag'] ) ? sanitize_text_field( $_GET['tag'] ) : '';
    
    	/** @var WP_Term $term */
    	foreach ( $terms as $term ) {
    		printf(
    			'<option value="%s"%s>%s (%d)</option>',
    			esc_attr( $term->slug ),
    			selected( $term->slug, $current, false ),
    			esc_html( $term->name ),
    			intval( $term->count )
    		);
    	}
    
    	echo '</select>';
    } );

    This seemed to work as expected on my testing site. I hope it’s what you’re looking for.

    Thread Starter electrolund

    (@electrolund)

    no problem! Thank you so much, this works wonderfully!

    Plugin Author Shea Bunge

    (@bungeshea)

    Glad to hear it!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘snippet to sort post list by tags’ is closed to new replies.