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'
),
    );
  • Sorry for the bump; but can someone please push me in the right direction? – TheBigK Oct 30 '16 at 2:26
-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

| improve this answer | |
  • Note that using query_posts() is not considered best practice anymore, check e.g. the harsh warnings in the Codex. – birgire Feb 22 '17 at 13:36
  • I know that but in some cases can be used. I not like it but can be helpful. – Ivijan Stefan Stipić Dec 4 '18 at 15:19

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.