Skip to content

Instantly share code, notes, and snippets.

@bradyvercher
Created February 24, 2013 20:06
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/5025368 to your computer and use it in GitHub Desktop.
Save bradyvercher/5025368 to your computer and use it in GitHub Desktop.
WordPress: Better Internal Link Search modifier to limit search results to a particular post type.
<?php
/**
* Search for a post type.
*
* <code>-cpt:{post_type} {query}</code>
*/
function bils_cpt_search( $results, $args ) {
$search_args = array(
'post_status' => 'any',
'post_type' => $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(
'title' => get_the_title( $post->ID ),
'permalink' => get_permalink( $post->ID ),
'info' => get_post_type( $post->ID ),
);
}
}
return $results;
}
add_filter( 'better_internal_link_search_modifier-cpt', 'bils_cpt_search', 10, 2 );
/**
* Add the help entry for searching CPTs.
*/
function bils_cpt_search_help( $results ) {
$results['cpt'] = array(
'title' => '<strong>-cpt:{post_type} {query}</strong></span><span class="item-description">Search for posts of a particular post type.</span>',
'permalink' => home_url( '/' ),
'info' => 'Local',
);
asort( $results );
return $results;
}
add_filter( 'better_internal_link_search_modifier_help', 'bils_cpt_search_help' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment