0

I am trying to exclude posts with a specific meta_value with a specific meta_key while also sorting the posts by a meta_value with a different meta_key.

This is my current code for the WP_Query class:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array( 
    'posts_per_page' => '50',
    'meta_key' => 'release_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'paged' => $paged
);
$the_query = new WP_Query($args);

the sorting by a specific meta_value part is working fine but I am not sure what to add to get it to exclude posts with a specific meta_value with a different meta_key than the one used for sorting.

Any advice / help is very much appreciated.

I got this working with the code below:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array( 
    'posts_per_page' => '50',
    'meta_key' => 'release_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'undetermined_date',
            'value' => 'TBA',
            'compare' => '!=',
            'type' => 'CHAR'
        )
    ),
    'paged' => $paged
);
$the_query = new WP_Query( $args );

I have changed the custom field type to check-boxes which returns true or false instead of a text box. How would I change this code for use with a check-boxes? Here is the code I have tried:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array( 
    'posts_per_page' => '50',
    'meta_key' => 'release_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'undetermined_date_toggle',
            'value' => 'true',
            'compare' => '==',
            'type' => 'CHAR'
        )
    ),
    'paged' => $paged
);
$the_query = new WP_Query( $args );
  • 1
    If this is the main query, use pre_get_posts rather than create a new query. – Milo Jun 5 '13 at 4:56
  • So is this still an open question? – fuxia Jun 5 '13 at 5:10
  • @Milo could you please explain how I would use pre_got_posts instead of meta_query for this? – RipzCurlz Jun 5 '13 at 7:30
  • @toscho This question is still open. – RipzCurlz Jun 5 '13 at 7:31
  • @RipzCurlz - it's not a question of pre_get_posts or meta_query, both would be meta queries, it's just a matter of where and when you're doing the query. if this is meant to replace the main query on an archive page, you should modify it before the query happens, which is before the template is loaded, this is done via pre_get_posts. – Milo Jun 5 '13 at 16:52
0

You code may be

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array( 
    'posts_per_page' => '50',
    'meta_key' => 'release_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'undetermined_date_toggle',
            'value' => 'true',
        )
    ),
    'paged' => $paged
);
$the_query = new WP_Query( $args );

this code show only those posts which have undetermined_date_toggle key==true or has checkbox check (selected) sorted by release_date_key.

Important Link:

WP_Query

*In meta_query parameter compare and type as your requirement not necessary to pass they have their default values = and CHAR, for more info read WordPress codex for WP_Query

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.