为什么我的Meta_Query没有运行?

时间:2013-07-21 作者:Adrian Halliday

仅供参考,我不是一个有经验的程序员,而且我对PHP的编码经验很少,可以肯定地说,我不知道基础知识,只是我在阅读、理解和编辑现有代码的过程中自学的内容。

我有一个网站,它有一个购买的主题,其中包括一个自定义的tax\\u查询,我复制了它的结构,以尝试按元数据中的不同字段过滤查询。我把我的函数放在函数中。我的子主题中的php文件,其中包含到目前为止我所做的所有修改。

这是我的代码:

function ah_meta_queries( $query = false ) {
    if ( ! ( is_post_type_archive( \'listing\' ) || is_tax( \'listing_type\' ) || is_tax( \'listing_location\' ) ) ) return;

    if ( is_admin() || ! is_a( $query, \'WP_Query\' ) || ! $query->is_main_query() ) return;

    if ( is_post_type_archive( \'listing\' ) ) {
        $meta_query_args = array( \'relation\' => \'AND\' );

        // For each type of Meta Data, check if filtering is set, then add it to the query array

        if ( isset( $_GET[\'et-listing-rating\'] ) && \'none\' != $_GET[\'et-listing-rating\'] ) {
        $meta_query_args[] = array(
                \'key\' => \'listing_type\',
                \'value\' =>  intval( $_GET[\'et-listing-rating\'] ),
        ); }

        if ( isset( $_GET[\'Fitness\'] ) )
        $meta_query_args[] = array(
                \'key\' => \'HN_Fitness\',
                \'value\' => \'TRUE\',
        );

        if ( isset( $_GET[\'Furnished\'] ) ) 
        $meta_query_args[] = array(
                \'key\' => \'HN_Furnished\',
                \'value\' => \'TRUE\',
        );

        if ( isset( $_GET[\'Kitchen\'] ) ) 
        $meta_query_args[] = array(
                \'key\' => \'HN_Kitchen\', 
                \'value\' => \'TRUE\',
        );

        if ( isset( $_GET[\'Parking\'] ) ) 
        $meta_query_args[] = array(
                \'key\' => \'HN_Parking\',
                \'value\' => \'TRUE\',
        );

        if ( isset( $_GET[\'Pets Allowed\'] ) ) 
        $meta_query_args[] = array(
                \'key\' => \'HN_Pets\',
                \'value\' => \'TRUE\',
        );

        if ( isset( $_GET[\'Pool\'] ) )
        $meta_query_args[] = array(
                \'key\' => \'HN_Pool\',
                \'value\' => \'TRUE\',
        );          

        // If user input Price-Low but no Price-High, make sure Listing Price Low or Listing Price High is more than Price-Low
        if ( (isset( $_GET[\'price-low\'] ) && \'\' !=$_GET[\'price-low\'] ) && !isset( $_GET[\'price-high\']) )
        $meta_query_args[] = array(
                \'relation\' => \'OR\',
                array(  
                    \'key\' => \'HN_Price_Low\',
                    \'value\' => $_GET[\'price-low\'],
                    \'type\' => \'numeric\',
                    \'compare\' => \'>=\', 
                ),
                array(
                    \'key\' => \'HN_Price_High\',
                    \'value\' => $_GET[\'price-low\'],
                    \'type\' => \'numeric\',
                    \'compare\' => \'>=\',
                ),
        );

        // If user input Price-High but no Price-Low, make sure Listing Price Low or Listing Price High is less than Price-High
        if ( !isset( $_GET[\'price-low\']) && (isset( $_GET[\'price-high\'] ) && \'\' !=$_GET[\'price-high\'] ) )
        $meta_query_args[] = array(
                \'relation\' => \'OR\',
                array(  
                    \'key\' => \'HN_Price_Low\',
                    \'value\' => $_GET[\'price-high\'],
                    \'type\' => \'numeric\',
                    \'compare\' => \'<=\', 
                ),
                array(
                    \'key\' => \'HN_Price_High\',
                    \'value\' => $_GET[\'price-high\'],
                    \'type\' => \'numeric\',
                    \'compare\' => \'<=\',
                ),
        );

        // If user input both Price-Low AND Price-High, make sure Listing Price Low or Listing Price High is BETWEEN Price-Low and Price-High
        if ( (isset( $_GET[\'price-low\'] ) && \'\' !=$_GET[\'price-low\'] ) && (isset( $_GET[\'price-high\'] ) && \'\' !=$_GET[\'price-high\'] ) )
        $meta_query_args[] = array(
                \'relation\' => \'OR\',
                array(  
                    \'key\' => \'HN_Price_Low\',
                    \'value\' => array( $_GET[\'price-low\'], $_GET[\'price-high\'] ),
                    \'type\' => \'numeric\',
                    \'compare\' => \'BETWEEN\', 
                ),
                array(
                    \'key\' => \'HN_Price_High\',
                    \'value\' => array( $_GET[\'price-low\'], $_GET[\'price-high\'] ),
                    \'type\' => \'numeric\',
                    \'compare\' => \'BETWEEN\',
                ),
        );



        // If any of the filters are set in the URL, then run the Meta Query

        if (    ( isset( $_GET[\'et-listing-rating\'] ) && \'none\' != $_GET[\'et-listing-rating\'] ) ||
                isset( $_GET[\'Fitness\'] ) ||
                isset( $_GET[\'Furnished\'] ) ||
                isset( $_GET[\'Kitchen\'] ) ||
                isset( $_GET[\'Parking\'] ) ||
                isset( $_GET[\'Pets Allowed\'] ) ||       
                isset( $_GET[\'Pool\'] ) ||       
                ( isset( $_GET[\'price-low\'] ) && \'\' !=$_GET[\'price-low\'] ) ||
                ( isset( $_GET[\'price-high\']) && \'\' !=$_GET[\'price-high\'] )
            )
            $meta_query = new WP_Meta_Query( $meta_query_args );            

    }
}
我的一些问题是:-为什么这个没有运行?是否必须将该函数设置为在其他任何地方运行-为什么函数是用参数($query=false)声明的?

欢迎提出任何建议!!

1 个回复
SO网友:Krzysiek Dróżdż

如果您没有将它分配给任何挂钩,并且没有调用它,那么它就无法工作-这个函数永远不会被调用。

您应该将其分配给pre_get_posts 我猜是胡克(至少看起来是这样)。

在函数后添加:

add_filter( \'pre_get_posts\', \'ah_meta_queries\' );
那么它应该可以正常工作了。

结束

相关推荐

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

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