wp query - WP_Query : Search and Filter Using custom field ANDOR custom taxonomy

admin2025-06-05  1

I've not been able to find my way using the existing answers; hence decided to post a new question anyway. What I want to achieve has probably been done 20,000 times; but I might just be an idiot. Anyway -

What I have: 1. Custom Post Type: 'job' 2. Each 'job' has: custom_filed_1: location , custom_field_2: company 3. It also has: custom_taxonomy_1: skills, custom_taxonomy_2: disciplines

For the life of me, I can't figure out how to write WP_QUERY that will help me retrieve posts that have: custom_field_1 AND custom_taxonomy_1 AND custom_taxonomy_2.

Like searching for jobs available in "New York" AND Skills "PHP + WordPress" AND available for "Computer Science Engineers".

My main confusion is how do I have 'AND' / 'OR' relationship between 'meta_query' and 'tax_query' arguments of WP_Query.

Would really appreciate your help. Thanks!

Here's my sample code:

    $args = array(
    'post_type' => array('job'),
    'post_status' => array('publish'),
    'posts_per_page' => '5',

     'meta_query' => array(
          'relation' => 'AND',
           array(
               'key' => 'location',
               'value' => $search_location, 
               'compare' => 'LIKE',
               ),
          array(
               'key' => 'company',
               'value' => $search_company, 
               'compare' => 'LIKE',
               ),     
        ),
     'tax_query' => array(
        'relation' => 'AND',
         array(
          'taxonomy' => 'skills',
          'field'    => 'slug', 
          'terms'    => $search_skills,
          'operator' => 'IN'
         ),
        array(
                 'taxonomy' => 'disciplines',
          'field'    => 'slug', 
          'terms'    => $search_disciplines,
          'operator' => 'IN'
),
    );

I've not been able to find my way using the existing answers; hence decided to post a new question anyway. What I want to achieve has probably been done 20,000 times; but I might just be an idiot. Anyway -

What I have: 1. Custom Post Type: 'job' 2. Each 'job' has: custom_filed_1: location , custom_field_2: company 3. It also has: custom_taxonomy_1: skills, custom_taxonomy_2: disciplines

For the life of me, I can't figure out how to write WP_QUERY that will help me retrieve posts that have: custom_field_1 AND custom_taxonomy_1 AND custom_taxonomy_2.

Like searching for jobs available in "New York" AND Skills "PHP + WordPress" AND available for "Computer Science Engineers".

My main confusion is how do I have 'AND' / 'OR' relationship between 'meta_query' and 'tax_query' arguments of WP_Query.

Would really appreciate your help. Thanks!

Here's my sample code:

    $args = array(
    'post_type' => array('job'),
    'post_status' => array('publish'),
    'posts_per_page' => '5',

     'meta_query' => array(
          'relation' => 'AND',
           array(
               'key' => 'location',
               'value' => $search_location, 
               'compare' => 'LIKE',
               ),
          array(
               'key' => 'company',
               'value' => $search_company, 
               'compare' => 'LIKE',
               ),     
        ),
     'tax_query' => array(
        'relation' => 'AND',
         array(
          'taxonomy' => 'skills',
          'field'    => 'slug', 
          'terms'    => $search_skills,
          'operator' => 'IN'
         ),
        array(
                 'taxonomy' => 'disciplines',
          'field'    => 'slug', 
          'terms'    => $search_disciplines,
          'operator' => 'IN'
),
    );
Share Improve this question asked Oct 28, 2016 at 13:49 TheBigKTheBigK 5691 gold badge7 silver badges17 bronze badges 1
  • Sorry for the bump; but can someone please push me in the right direction? – TheBigK Commented Oct 30, 2016 at 2:26
Add a comment  | 

1 Answer 1

Reset to default -1

Maby this example can help:

<?php

global $wp_query; // get the global object

$thesearch = get_search_query(); // get the string searched

// merge them with one or several meta_queries to meet your demand
$args = array_merge( $wp_query->query, array( 
   'meta_query' => array(
    array(
        'key' => 'field_to_seach',
        'value' => $thesearch,
        'compare' => 'IN'
    )
)
    ));
query_posts( $args ); // alter the main query to include your custom parameters

?>

UPDATE:

This is realy bad practice but in the some custom works you can use it in some realy rare occasion.

Alos read this note:

Note: This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible. In most cases, there are better, more performant options for modifying the main query such as via the ‘pre_get_posts’ action within WP_Query.

This must not be used within the WordPress Loop.

More about it here

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749129343a316612.html

最新回复(0)