Skip to content

Instantly share code, notes, and snippets.

@bradyvercher
Created February 25, 2013 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradyvercher/5033090 to your computer and use it in GitHub Desktop.
Save bradyvercher/5033090 to your computer and use it in GitHub Desktop.
WordPress: Better Internal Link Search modifier to search for posts within a particular category. http://wordpress.org/support/topic/filter-by-categories
<?php
/**
* Search for posts in a category.
*
* <code>-categ:{category slug} {query}</code>
*/
function bils_category_search( $results, $args ) {
$post_types = get_post_types( array( 'public' => true ), 'objects' );
$search_args = array(
'post_status' => 'any',
'post_type' => array_keys( $post_types ),
'category_name' => $args['modifier'][1],
'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' => $post_types[ $post->post_type ]->labels->singular_name,
);
}
}
return $results;
}
add_filter( 'better_internal_link_search_modifier-categ', 'bils_category_search', 10, 2 );
/**
* Add the help entry for searching posts in a category.
*
* Multiple categories can be searched by separating their slugs with a comma.
*/
function bils_categ_search_help( $results ) {
$results['categ'] = array(
'title' => '<strong>-categ:{category slug} {query}</strong></span><span class="item-description">Search your local WordPress installation for posts of that category.</span>',
'permalink' => home_url( '/' ),
'info' => 'Local',
);
asort( $results );
return $results;
}
add_filter( 'better_internal_link_search_modifier_help', 'bils_categ_search_help' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment