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 );
5
  • 1
    If this is the main query, use pre_get_posts rather than create a new query.
    – Milo
    Commented Jun 5, 2013 at 4:56
  • So is this still an open question?
    – fuxia
    Commented Jun 5, 2013 at 5:10
  • @Milo could you please explain how I would use pre_got_posts instead of meta_query for this?
    – RipzCurlz
    Commented Jun 5, 2013 at 7:30
  • @toscho This question is still open.
    – RipzCurlz
    Commented Jun 5, 2013 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
    Commented Jun 5, 2013 at 16:52

1 Answer 1

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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