I am trying to set 'meta_query' programaticaly for a filter which is supposed to filter a custom post type depending on the existence of a metakey. I have tried several ways, which I have left in the below code, commented out. None of the solutions change the actual $query 'meta_query' property, which stubbornly remains set to 'false'.
I have a feeling that i'm missing something basic. Please assist. Thank you.
add_filter( 'parse_query', 'filter_posts_by_no_associated_contact' );
function filter_posts_by_no_associated_contact( $query ) {
global $pagenow;
$qv = &$query->query_vars;
if( $pagenow == 'edit.php' &&
isset($qv['post_type']) && $qv['post_type'] == 'act'
&& isset($_GET['contact_status']) && $_GET['contact_status'] != '' ){
$compare = ( $_GET['contact_status'] == 'with_contact' ) ? 'EXISTS' : 'NOT EXISTS';
// $query->meta_query[] = array(
$metaquery = array(
'key' => 'contact_associated',
'value' => 'justAstring',
'compare' => $compare,
'type' => 'CHAR'
);
$query->set( 'meta_query', $metaquery );
}
}
i have seen this done elsewhere:
$qv = &$query->query_vars;
$qv['meta_query'] = array();
$qv['meta_query'][] = array(
'field' => 'meta-key',
'value' => $whatever,
'compare' => '',
'type' => ''
);
But this solution results in a php "Warning: Attempt to modify property of non-object".