• Hi – I am trying to modify the following WP_query filter (which works) to use OR for filtering meta_key, meta_value, and meta_compare.

    add_filter( 'posts_where', 'filter_where' );
    $slide = new WP_query(array('cat' => $parentid ));
    remove_filter( 'posts_where', 'filter_where' );
    function filter_where( $where = '' ) {
        $where .= " AND comment_count > 0 ";
        return $where;
    }

    Below is the best I’ve come up with and I have tried many variations, including trying to make a call without the “comment_count >0” included, but I can’t get it to return anything.

    function filter_where( $where = '' ) {
    	global $wpdb;
        $where .= " AND comment_count > 0 OR ($wpdb->postmeta.meta_key = 'social_stream' AND $wpdb->postmeta.meta_value = '22' AND $wpdb->postmeta.meta_compare = '>=') ";
        return $where;
    }

    Any help will be greatly appreciated. Thanks.

Viewing 1 replies (of 1 total)
  • I think you will need to add a join filter to join the postmeta table to the query before you can test for the values. Dump out the request so you can see the actual sql used for the query (untested):

    add_filter( 'posts_where', 'filter_where' );
    $slide = new WP_query(array('cat' => $parentid ));
    print_r('<pre>REQUEST:');print_r($slide->request);print_r('</pre>');
    remove_filter( 'posts_where', 'filter_where' );
Viewing 1 replies (of 1 total)

The topic ‘posts_where Filter for Comment Count and Postmeta’ is closed to new replies.