Use meta_query w/Custom Fields

This topic contains 2 replies, has 2 voices, and was last updated by  jonahcoyote 10 years, 2 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #16912

    jonahcoyote
    Expired Member
    Post count: 89

    Hi guys,

    I’m wanting to modify some of my GD queries using pre_get_posts and a custom field value but I realized the custom fields are stored in their own tables related to their CPT.

    Here’s the code I have so far:

    
    
    
    /*-------------------------------------------------------------------------------
    	Show More Posts Per Page & Filter Out Sold Listings
    -------------------------------------------------------------------------------*/
    function show_more_posts($query) {
    	if ( empty( $query->query_vars['suppress_filters'] ) ) {
    		if( geodir_is_page('listing') || geodir_is_page('author') || geodir_is_page('search') ) {
    			$query->set( 'posts_per_page', -1 );
    			$query->set( 'meta_query', array(
    				'relation' => 'OR',
    				array(
    					'key' => 'geodir_is_sold',
    					'value' => 1,
    					'compare' => '=',
    					'type' => 'BINARY'
    				),
    				array(
    					'key' => 'geodir_is_sold',
    					'compare' => 'NOT EXISTS',
    					'value'   => 'completely'
    				)
    			)
    			);
    		}
    	}
    	return $query;
    }
    add_filter( 'pre_get_posts' , 'show_more_posts' );
    

    Do you guys know how I could modify the query in the way I’m try to?

    #16966

    Jeff
    Buyer
    Post count: 188

    Hi Jonah:

    The short answer is “No”, not using pre_get_posts as WP_Query won’t understand the extra tables. It’s one thing I really don’t like about the GeoDirectory architecture.

    That said, have a look at some of the other filter options, like “posts_where” and related.

    http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where

    #16998

    jonahcoyote
    Expired Member
    Post count: 89

    Thanks Jeff, that’s what I figured. I’ll check out the filter options.

Viewing 3 posts - 1 through 3 (of 3 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount