WP_QUERY-使用多个SELECT按TAX_QUERY和META_QUERY筛选

时间:2014-06-19 作者:user1575949

我有一本出版物Post Type, a编剧Taxonomy 和出版物自定义“元框”值;parent_id.

在我的archive-publictions-post.php 模板我正在尝试筛选WP_Query 类,具有tax_querymeta_query 参数。

使用下面的代码,我可以在页面加载时显示所有出版物,当我同时选择一个术语和一个父项时,我可以获得过滤结果,但当我单独选择时,我不能得到过滤结果?

我不确定meta_query &;tax_query relationship 在这种情况下应使用参数?

<?php
  // The code from: archive-publictions-post.php

  // Set Var Filter
  $publications_filter = $_POST[\'publications\'];

  // Set Terms Arguments
  $term_args = array(
    \'orderby\' => \'name\',    
    \'order\' => \'ASC\',     
    \'hide_empty\' => false,    
    \'parent\' => 0,      
  );
  // Get Publications Terms
  $terms = get_terms(\'publications-tax\', $term_args);

  $writers_args = array( 
    \'post_type\' => \'writers-post\', 
    \'posts_per_page\' => -1,
    \'post_status\' => \'any\',
    \'order\' => \'DESC\',
  ); 
  // Get Writers Posts
  $writers = get_posts($writers_args);

    echo \'<select name="publications[]" multiple>\';
      // Set var as an array
      $all_writers = array();
      foreach($writers as $writer){
        // Set all terms to our array
        $all_writers[] = $writer->ID;
        echo \'<option value="\'.$writer->ID.\'">\'.$writer->post_name.\'</option>\';
      }
      foreach($terms as $term){
        // Set all terms to our array
        $all_terms[] = $term->name;
        echo \'<option value="\'.$term->name.\'">\'.$term->name.\'</option>\';
      }          
    echo \'</select>\';         
    echo \'<input type="submit" value="go">\';
  ?>
  </form>
WP_QUERY $args:

   //Start Args

    $publications_results = isset($_POST[\'publications\']) ? $publications_filter : $all_terms;

    $publication_values=array();
    $writers=array();
    foreach ($publications_results as $key => $value) {
      if (is_numeric($value)) {
        $writers_values[] = $value;
      }
      else{
        $publications_values[] = $value;
      }
    }
    $args = array(   
      \'post_type\' => array(\'publications-post\',\'writers-post\'),
      \'order\' => \'DESC\',
      \'post_status\' => \'any\',
      \'orderby\' => \'date\',
      \'tax_query\' => array(array(
        \'taxonomy\' => \'publications-tax\',
        \'field\' => \'slug\',
        \'terms\' => $publications_values
      ))
    ); 

    if($_POST[\'publications\']){
      $args[\'meta_query\'][] = 
      array(
        \'key\' => \'parent_id\',
        \'value\' => $writers_values,
        \'type\' => \'numeric\',
      );
    }
谢谢

1 个回复
SO网友:user1575949

我的初始代码没有提供默认值WP_query 参数参数如果未设置任何一个Select选项-空参数将过滤掉所有内容。

尽我所能,我应用了初始Post和Terms查询中的默认参数值,现在select过滤器正在工作。

我很高兴知道我的代码是否可以改进:)

  /*
  *Get Publications Filters 
  */

  $writers_filter = array();
  $publications_filter = array();

  if (isset($_POST[\'publications\'])) {

    $publications_filter = $_POST[\'publications\'];
    // Separate Numeric from String - i.e. Post ID and Term Name
    foreach ($publications_filter as $key => $value) {
      if (is_numeric($value)) {
        $writers_filter[] = $value;
      }
      else{
        $terms_filter[] = $value;
      }
    }
  }      

  // If Either Select Option is Empty, 
  // Assign Final Value With All Queried Results - Avoid Empty Value 

  $final_writers = array();
  $final_terms = array();
  $final_writers = (!empty($writers_filter)) ? $writers_filter : $all_writers;
  $final_terms = (!empty($terms_filter)) ? $terms_filter : $all_terms;

  /*
  *Set WP_Query Argument Parameters
  */

  $args = array(   
    \'post_type\' => array(\'publications-post\',\'writers-post\'),
    \'order\' => \'DESC\',
    \'post_status\' => \'any\',
    \'orderby\' => \'date\',
    \'tax_query\' => array(array(
      \'taxonomy\' => \'publications-tax\',
      \'field\' => \'slug\',
      \'terms\' => $final_terms
    ))
  ); 

  if($_POST[\'publications\']){
    $args[\'meta_query\'][] = 
    array(
      \'key\' => \'parent_id\',
      \'value\' => $final_writers,
      \'type\' => \'numeric\',
    );
  }

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post