Skip to content

Instantly share code, notes, and snippets.

@MZAWeb
Created July 28, 2012 21:23
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 MZAWeb/3194851 to your computer and use it in GitHub Desktop.
Save MZAWeb/3194851 to your computer and use it in GitHub Desktop.
Exclusive search for p2p
<?php
add_action( 'pre_get_posts', 'query_for_recipe_search' );
function query_for_recipe_search( $query ) {
if ( is_recipe_search() && is_main_query() ) {
$ingredients = isset( $_GET["search_ingredients"] ) ? $_GET["search_ingredients"] : NULL;
if ( is_array( $ingredients ) ) {
global $wpdb;
$recipes = $wpdb->get_col( $wpdb->prepare( "SELECT p1.id from wp_posts p1
INNER JOIN wp_p2p p2p on p2p.p2p_from=p1.id
where p2p_to IN (%s)
GROUP BY p1.id
HAVING COUNT(p2p.p2p_to) = %d", join( "','", $ingredients ), count( $ingredients ) ) );
if ( $recipes ) {
$query->query_vars["post__in"] = $recipes;
}
}
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment