如果存在,则追加到现有WP_Query的META_QUERY

时间:2019-08-12 作者:Ryan Griffith

我想加入pre_get_posts 改变WP_Query 并为meta_query 部分我希望我的钩子对可能修改此键的任何其他钩子都是非破坏性的。

确定是否设置了此键的最合适方法是什么,如果设置了,则适当地向其附加一个AND 在不影响其他插件之前所做的任何修改的情况下。

1 个回复
SO网友:HU ist Sebastian

您可以使用WP\\u Query::get方法获取元查询中的任何内容,如下所示:

add_action(\'pre_get_posts\',\'add_another_meta_query\');

function add_another_meta_query($query){
  //Be sure to only use the main query on frontend
  if($query->is_main_query() && !is_admin()){
    $meta_query = $query->get(\'meta_query\',array());
    $new_meta_query = array();
    //now, $meta_query has the meta_query if set (or an array)
    //check if the old version of meta_query is used
    if($meta_key = $query->get(\'meta_key\',NULL)){
       $old_type_of_meta = array();
       $old_type_of_meta[\'key\'] = $meta_key;
       $query->unset(\'meta_key\');
       if($meta_value = $query->get(\'meta_value\',NULL)){
        $old_type_of_meta[\'value\'] = $meta_value;
        $query->unset(\'meta_value\');
       } elseif($meta_value = $query->get(\'meta_value_num\',NULL)){
         $old_type_of_meta[\'value\'] = $meta_value;
         $query->unset(\'meta_value_num\');
       } else {
         $old_type_of_meta[\'compare\'] = \'EXISTS\';
       }
       if($meta_compare = $query->get(\'meta_compare\',NULL)){
         $old_type_of_meta[\'compare\'] = $meta_compare;
         $query->unset(\'meta_compare\');
       }
       if($meta_type = $query->get(\'meta_type\',NULL)){
        $old_type_of_meta[\'type\'] = $meta_type;
        $query->unset(\'meta_type\');
       }
       $new_meta_query[] = $old_type_of_meta;
       if($orderby = $query->get(\'orderby\')){
         if(!is_array($orderby)){
            $orderby = str_replace(array(\'meta_value\',\'meta_value_num\'),array($meta_key,$meta_key),$orderby);
            $query->set(\'orderby\',$orderby);
         }
       }
    }
    if(isset($meta_query[\'relation\'])){
        //there is a special relation set within the meta_query.
        //to preserve this, we have to encapsulate the old meta queries
        $new_meta_query[] = $meta_query;
    } else {
       //there is no special relation set on global level ("AND" is used)
       //we can just merge them all together
       foreach($meta_query as $old_single_meta_query){
          $new_meta_query[] = $old_single_meta_query;
       }
    }
    //now we got all the "old" meta_queries in our $new_meta_query array.
    //let\'s add our own!
    $new_meta_query[] = array(
                        \'key\' => \'my_awesome_meta_key\',
                        \'value\' => \'whatever_i_need\',
                        \'compare\' => \'=\',
                        \'type\' => \'\',
                        );  
    //Last but not least: put it back into the $query
    $query->set(\'meta_query\',$new_meta_query); 
  }
}
快乐的编码!

相关推荐

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

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