$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - WP_Query : Search and Filter Using custom field ANDOR custom taxonomy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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

Post a comment

comment list (0)

  1. No comments so far