The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Using 'meta_query' to filter Posts (2 posts)

  1. scarekrow76
    Posted 4 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(
    		'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
    Posted 4 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