Support » Plugin: Better Internal Link Search » Filter by Categories

  • Resolved Sophie G

    (@sophie-g)


    Hi,
    First, I like what your plugin does and I want to thank you for making it available to us.

    Here’s my problem. I’d like to add a modifier that allows me to search for posts/pages by categories. Something like -categ {category name}. Being somewhat limited in PHP I tried my hand at it but I’m not having too much success. Would you mind helping me please?

    Here’s what I wrote:

    add_filter( 'better_internal_link_search_modifier-categ', 'bils_category_search', 10, 2 );
    function bils_category_search( $results, $args ) {
    		$pts = get_post_types( array( 'public' => true ), 'objects' );
    		$pt_names = array_keys( $pts );
    		$categories = get_terms( 'category' );
    		$searched_cat = $args['s'];
    
     if ( $searched_cat == $categories->name){
    	  $cat = $categories->term_id;
      }
    
    	$search_args = array(
    		'post_status' => 'any',
    		'post_type' => $pt_names,
    		'category' => $cat,
    		'paged' => $args['page'],
    		'posts_per_page' => $args['per_page'],
    		's' => $args['s']
    	);	
    
    	$posts = get_posts( $search_args );
    	if ( $posts ) {
    		foreach ( $posts as $post ) {
    			$results[] = array(
    				'ID' => $post->ID,
    				'title' => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
    				'permalink' => get_permalink( $post->ID ),
    				'info' => $pts[ $post->post_type ]->labels->singular_name,
    			);
    		}
    
    	}
    	return $results;
    
    }

    I also mofified the help section part by adding a part in the array:
    `’categ’ => array(
    ‘title’ => ‘<strong>-categ {query}</strong></span><span class=”item-description”>Search your local WordPress installation for posts of that category.</span>’,
    ‘permalink’ => home_url( ‘/’ ),
    ‘info’ => ‘Local’
    ),`

    http://wordpress.org/extend/plugins/better-internal-link-search/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Brady Vercher

    (@bradyvercher)

    Hi Sophie,

    You were close! It sounds like you want to search for posts within a particular category, so you need to input a couple of things: The category and the search query. Here’s a snippet based on your code that does this. And here’s the syntax:

    -categ:{category slug} {query}

    If needed, you can also search across multiple categories by separating their slugs with commas: -categ:{cat1,cat2,cat3,etc}

    I also included the help text via a filter so that your modifications won’t be wiped out when the plugin is updated in the future. I’d recommend dropping the new snippet from the link above in a separate plugin of your own, but it will work in your theme’s functions.php file as well.

    – Brady

    Thread Starter Sophie G

    (@sophie-g)

    Thanks for the quick reply! I really appreciate it.

    You code works like a charm!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Filter by Categories’ is closed to new replies.