Support

Account

Home Forums General Issues Custom query for post and postmeta values

Helping

Custom query for post and postmeta values

  • I have read the article about custom queries but it doesn’t quite answer my problem and I seem to be going round in circles trying to get a custom query that does what I want.

    I want to search for a name. This name may be the title of the custom post or it may be in one of up to 9 custom fields in the CPT. There may also be multiple occurrences of this name in either of the location types.

    I want to search through the CPT and then display the results in a custom template. I have found this easy to do if the name is in the post title or in the meta_value. However, I can’t seem to get it all hanging together.

    Any query experts who can help please?

  • If I understood correctly you can try this. Just insert each field as array line after ‘relation’ and when use wp_query loop.

    $searchkey = 'yourwordhere';
    $args = array(
      'post_type'=> 'cpt_title',
      'posts_per_page' => -1,
      'meta_query' => array(
      'relation' => 'OR',
       array('key' => 'acf_field_name_here1','value' => $searchkey,'compare' => 'LIKE'),
       array('key' => 'acf_field_name_here2','value' => $searchkey,'compare' => 'LIKE')
       )    
    );
    
    $wp_query = new WP_Query($args);
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Custom query for post and postmeta values’ is closed to new replies.