Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

php - WP_Query - relation issue

I am setting a WP_Query that runs through all the posts. The example below I want it to show the users selected 'industry', 'access type' and 'location'. This is working perfectly for 'industry' and 'access type' however I have recently added 'location' and it is only showing the scripts from the location.

However I want to show ALL posts in the users selected 'industry' and 'access_type', then I want to show the 'location' as an extra, and not to replace so it just shows posts with the users location. The location is more of an extra.

I have tried using the relation as 'OR' and nested AND within, but this completely breaks.

Could someone please help, very stuck with this!

    <?php 
    $wpb_all_query = new WP_Query(array(
      $user_id = $current_user->ID,
      $key = 'user_registration_occupation',
      $keyi = 'user_registration_industries',
      $keyii = 'user_registration_location',            
      $single = true,
      $user_last = get_user_meta( $user_id, $key, $single ),
      $user_lasti = get_user_meta( $user_id, $keyi, $single ),      
      $user_lastii = get_user_meta( $user_id, $keyii, $single ),
        
        'post_type' => array('post', 'case_studies'),
        'orderby' => 'publish_date',
        'order' => 'DEC',
        $string = preg_replace('/[^p{L}p{N}s]/u', '', $user_last),               
        'category_name' => $string,
        'tax_query' => array(
        'relation' => 'AND',
            
            array(
                'taxonomy' =>  'industry',
                'field'    => 'slug',
                $string2 = preg_replace('/[^p{L}p{N}s]/u', '', $user_lasti),
                'terms' => $user_lasti,
            ),              
            array(
                'taxonomy' => 'access_type',
                'field'    => 'slug',
                'operator' => 'IN',
            ),
            array(
                'taxonomy' => 'location',
                'field'    => 'slug',
                $string3 = preg_replace('/[^p{L}p{N}s]/u', '', $user_lastii),
                'terms' => $user_lastii,
            ),

        ),

        
        'post_status'=>'publish', 'posts_per_page'=>-1));   
    ?>
question from:https://stackoverflow.com/questions/66066899/wp-query-relation-issue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...