I have a search form for a custom post type. The user should be able to search by name of the store and the city where the store is located The name of the store is the same as the title of the post, so Ive managed to figure that part out. The city on the other hand is an advanced custom field, and I can't figure out how to include this in the search query. So when the user starts to type, is it a match with 1. the post title and/or 2. the advanced custom field "city"?

This is my code in functions.php

add_action( 'wp_footer', 'ajax_fetch' );
function ajax_fetch() { ?>

<script type="text/javascript">
 function fetch(){

        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
        success: function(data) {
        jQuery('#datafetch').html( data );


 // the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');

function data_fetch(){
    if (  esc_attr( $_POST['keyword'] ) == null ) { die(); }
    $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'aterforsaljare' ) );
    if( $the_query->have_posts() ) :
        while( $the_query->have_posts() ): $the_query->the_post(); ?>

            <h2><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h2>

        <?php endwhile;


And the html is as simple as this:

<input type="text" name="keyword" id="keyword" onkeyup="fetch()"></input>

<div id="datafetch">Search results will appear here</div> 

This code works as long as the user search for the title of store. How do I manage to include a custom field as an argument (sorry this might be the wrong word but I cant find the right one) to the WP_query?

I found this example, and I tried it but just got an error saying that cf_search_join couldn't be found. And at the same time didn't really manage to understand the code... Is this the more right approach? Or can I build something from what I already have?


$the_query = new WP_Query( array( 
    'posts_per_page' => -1, 
    's' => esc_attr( $_POST['keyword'] ), 
    'post_type' => 'aterforsaljare',
    'meta_query' => array(
        'relation' => 'OR',
            'key' => 'vendor_city',
            'value' => 'value',
            'compare' => 'LIKE',
) );
  • 1
    Keep in mind this is going to be a very expensive query. If you want this to work quickly, and work well, you should look into setting up elastic search and using a plugin to integrate with it – Tom J Nowell Jan 31 '18 at 15:55

You can include meta_query parametr in your WP_Query.

'meta_query' => array(
    'relation' => 'OR',
        'key' => 'country',
        'value' => 'value',
        'compare' => 'LIKE',
        'key' => 'city',
        'value' => 'value',
        'compare' => 'LIKE',
  • After adding this to my WP_Query, no results is given instead.. Not even if I search for the name. – teninchhero Feb 1 '18 at 5:42
  • Show your query. It should work propertly. – Maxim Sarandi Feb 1 '18 at 8:17
  • I have updated my post with the query – teninchhero Feb 1 '18 at 9:10
  • Are you changed value on your request variable? Should be 'value' => $_POST['keywords'] – Maxim Sarandi Feb 1 '18 at 9:11
  • Okay I was a bit ahead of myself. Now it doesn't go for the title, only the city. I changed "value" to $_POST['keywords'] in the meta_query and left the rest of the code as in my example above. – teninchhero Feb 1 '18 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.