public
Created

Exclusive search for p2p

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.