Skip to content

Instantly share code, notes, and snippets.

@PCianes
Last active July 8, 2017 22:41
Show Gist options
  • Save PCianes/d132d6ac20fdc83f9df8ca21fda92b2c to your computer and use it in GitHub Desktop.
Save PCianes/d132d6ac20fdc83f9df8ca21fda92b2c to your computer and use it in GitHub Desktop.
Filter the query: filter content of posts accoring to post title and meta value
<?php
// To plugin or functions.php
add_filter( 'posts_where', 'your_title_func', 10, 2 );
function your_title_func( $where, &$the_query )
{
global $wpdb;
if ( $search_title = $the_query->get( 'search_title' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( like_escape( $search_title ) ) . '%\'';
}
return $where;
}
// To the loop
$postcode = $_POST['postcode'];
$title = $_POST['term'];
$args = array(
'post_type' => 'product',
'search_title' => $title,
'meta_query' => array(
array( 'key' => 'custom_field_ID_1',
'value' => $postcode,'compare' => 'LIKE' )
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment