• Hi.

    I sent you the link in the attachment so you can see that menu disappears on custom filter change below. If you choose ‘most recent’ (that not affects meta_key) menu shows nicely, but if you choose price, all menus disappear.

    Here is the code:

    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;

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    All menu items also pass through the pre_get_posts action. Your callback is altering menu queries as well as the intended property query. Since your changes are invalid for menus, no menu items are returned and the menu disappears.

    Besides excluding admin, main, and singular queries, you need to exclude all other queries besides the one you want to affect. Or taken another way, only apply your changes to the specific query it should be applied to. There’s usually some property or combination thereof in the query that uniquely identifies it from all others. You could identify what they are and ensure they exist before changing the query.

    Because you are already excluding main queries, that tells me the page content is from a custom query. If that’s the case, you could wait to add your action callback until just before it’s needed, right on the template. If need be, it can then be removed after it’s used for the custom query so it does not affect subsequent queries. The function declaration can stay where ever it is, only the add_action() call needs to be moved.

    Thread Starter vonkoga

    (@vonkoga)

    Thank you for quick response. I’m just curious how ‘Recent first’ filter works, since they are the part of the same query, except that ‘Recent first’ doesn’t affect meta_key values.

    Can you help me to adapt the code above just as a workaround, i don’t need anything elegant

    Moderator bcworkz

    (@bcworkz)

    The quick response was a mere coincidence. As you’ve now discovered, I’m not hovering over posts, ready to pounce on new ones. I generally “drive by” daily, but I am generally not paying attention in between.

    The recent first filter is part of your noo-landmark theme, so I’m not very knowledgeable about the specifics. From what your code does, I can infer that the selection is stored in the options table as part of a “theme_mods” array of data. Based on the value saved, different orderby arguments are set for the query.

    I’m making a large deductive leap in assuming the query that needs to be modified is a new custom query instantiated right on the page’s template because your code is skipping over any main queries. On the page’s template, there ought to be a call to new WP_Query(), get_posts(), or query_posts(). Locate one of these on the page’s template. Once located, move the add_action() line of your code from where it is to somewhere on the page template before whichever of those query functions are used. Move the one line only, the rest may remain where it is.

    Be sure where you add the add_action() line is within a <?php ?> block, even if you need to make your own. If there are other custom queries run after the one for properties, (in a sidebar or footer widget for example) you’ll need to add a remove_action() call after the query is instantiated to prevent your callback from influencing the subsequent queries.

    Thread Starter vonkoga

    (@vonkoga)

    Thank you very much

    Moderator bcworkz

    (@bcworkz)

    No problem. Let us know how it works out.

    Oh — I should have mentioned that where you add the line before the query is instantiated also needs to be after the call to wp_nav_menu(). That should normally happen automatically with most themes because the wp_nav_menu() call is on a different, earlier template. Not all themes follow the norms.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Menu disappears when meta_key changed’ is closed to new replies.