WordPress.org

Ready to get started?Download WordPress

Forums

Using 'meta_query' to filter Posts (2 posts)

  1. Ryan Ball
    Member
    Posted 2 years ago #

    To allow for easy filtering of Posts with a specific Custom Field defined I've added a 'meta_query' parameter to the query via the 'pre_get_posts' filter.

    $query->set( 'meta_query', array(
    	array(
    		'key' => '_keyresource',
    		'value' => 1,
    		'compare' => '==',
    		'type' => 'numeric'
    		)
    	)
    );

    Is there any way I can limit this additional parameter to the main Post loop only?

  2. vtxyzzy
    Member
    Posted 2 years ago #

    One solution I have used to turn a filter on and off is to use a global variable.

    I set the global variable just before issuing the query that I want filtered and then clear it just after the query.

    I check for the global variable in the filter function and only filter if it is set.

Topic Closed

This topic has been closed to new replies.

About this Topic