更改META_KEY时菜单消失

时间:2018-02-22 作者:SemperLabs

Menu disappears example

我给你发送了附件中的链接,这样你就可以看到菜单在下面的自定义过滤器更改中消失。如果选择“最近”(不影响meta\\U键),菜单会很好地显示,但如果选择“价格”,所有菜单都会消失。

代码如下:

if ( ! function_exists( \'noo_landmark_addon_pre_get_posts\' ) ) :

    function noo_landmark_addon_pre_get_posts( $query ) {
        if ( is_admin() ) {
            return $query;
        }
        if ( $query->is_main_query() && $query->is_singular ) {
            return;
        }

        /**
         * Set query in archive property
         */
        if ( RP_Property::is_property() ) {

            $args = array();

            /**
             * Check order
             */
            $default_orderby = isset( $query->query_vars[ \'orderby\' ] ) ? $query->query_vars[ \'orderby\' ] : get_theme_mod( \'noo_property_listing_orderby\', \'date\' );

            $orderby = isset( $_GET[ \'orderby\' ] ) ? sanitize_text_field( $_GET[ \'orderby\' ] ) : $default_orderby;

            $orderby            = strtolower( $orderby );
            $order              = isset( $query->query_vars[ \'order\' ] ) ? $query->query_vars[ \'order\' ] : \'DESC\';

            $args[ \'orderby\' ]  = $orderby;
            $args[ \'order\' ]    = $order == \'DESC\' ? \'DESC\' : \'ASC\';
            $args[ \'meta_key\' ] = \'\';

            // global filter

            if ( isset($_GET[\'recent_first\']) ) {
                    $args[ \'orderby\' ] = \'date\';
                    $args[ \'order\' ]   = sanitize_text_field($_GET[\'recent_first\']);
            }
            if ( isset($_GET[\'property_price\']) ) {
                    $args[ \'orderby\' ]  = \'meta_value_num meta_value\';
                    $args[ \'order\' ]    = sanitize_text_field($_GET[\'property_price\']);
                    $args[ \'meta_key\' ] = \'price\';
            }

            $query->set( \'orderby\', $args[ \'orderby\' ] );
            $query->set( \'order\', $args[ \'order\' ] );

            if ( isset( $args[ \'meta_key\' ] ) && ! empty( $args[ \'meta_key\' ] ) ) {
                $query->set( \'meta_key\', $args[ \'meta_key\' ] );
            }
            if ( isset( $args[ \'meta_value\' ] ) && ! empty( $args[ \'meta_value\' ] ) ) {
                $query->set( \'meta_value\', $args[ \'meta_value\' ] );
            }
            if ( isset( $args[ \'meta_query\' ] ) && ! empty( $args[ \'meta_query\' ] ) ) {
                $query->set( \'meta_query\', $args[ \'meta_query\' ] );
            }

            /**
             * Set number show posts
             */
            $property_per_page = get_theme_mod( \'noo_property_per_page\', 10 );
            $query->set( \'posts_per_page\', $property_per_page );
            $query->set( \'post_status\', \'publish\' );

        }

        /**
         * Set query in archive agent
         */
        if ( RP_Agent::is_archive_agent() ) {
            $agent_per_page = get_theme_mod( \'noo_agent_per_page\', 10 );
            $query->set( \'posts_per_page\', $agent_per_page );

            $agent_must_has_property = Realty_Portal::get_setting( \'agent_setting\', \'agent_must_has_property\', false );
            if ( $agent_must_has_property === \'1\' ) {

                query_posts( \'post_type=noo_agent&posts_per_page=-1\' );
                $agent_ids = array();
                while ( have_posts() ) : the_post();
                    $agent_id = get_the_ID();
                    $user_id  = RP_Agent::get_id_user( $agent_id );
                    if ( $user_id < 1 ) {
                        continue;
                    }
                    $total_property = count_user_posts( $user_id, \'noo_property\' );
                    if ( $total_property > 0 ) {
                        $agent_ids[] = $agent_id;
                    }
                endwhile;
                wp_reset_query();
                $query->set( \'post__in\', $agent_ids );
            }
        }
    }

    add_action( \'pre_get_posts\', \'noo_landmark_addon_pre_get_posts\' );
endif;

1 个回复
SO网友:Peter HvD

if ( $query->is_main_query() && $query->is_singular ) {
            return;
}
这是错误的做法-它实际上是在确保除主查询之外的所有查询都被修改,这就是它影响菜单(也使用查询构建)的原因。

您需要将其更改为if ( !$query->is_main_query() && !$query->is_singular )

结束

相关推荐