0

I have a loop in my index.php for my home page like this...

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>

I have a function to query the posts by ascending date...

function order_home_asc($query) {
  if ($query->is_home() && $query->is_main_query()) {
    $query->set('orderby', 'date');
    $query->set('order', 'asc');
  }
}
add_action('pre_get_posts', 'order_home_asc');

I've been told the above is the best way to query the posts.

I now want to query those posts by a specific post meta key('job_status') and value('open') as well as by date and asc.

I tried this but it didn't work...

function order_home_asc($query) {
  if ($query->is_home() && $query->is_main_query()) {
    $query->set('orderby', 'date');
    $query->set('order', 'asc');
    $query->set('meta_query, array( 
    array( 'key', 'job_status', 'value', open, 'compare', '!=' )
    )
  }
}
add_action('pre_get_posts', 'order_home_asc');

If there's an easier/simpler solution without the need for a function that would be great.

Would this be better as my loop?

<?php
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title', 'meta-key' => 'job_status','meta_value' => 'open' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
  setup_postdata( $post ); ?> 
        <?php the_title(); ?>   
<?php
endforeach; 
wp_reset_postdata();
?>
3

Your syntax is complete wrong in your meta_query. A key/value pair in an array should be in the format of 'key' => 'value'. Your format is 'key', 'value' which in context of an array translate to 0 => 'key', 1 => 'value'. That is why your meta_query does not work.

Your failure is also due to not very good housekeeping. As your meta_query is written, it is hard to properly read it and because of that you miss obvious bugs.

Lets rewrite that section in a more readable way

$meta_query = [
    [
        'key'     => 'job_status',
        'value'   => 'open',
        'compare' => '!='
    ]
];
$query->set( 'meta_query', $meta_query );

As you can see, my code is easy to read, and very easy to spot obvious bug.

EDIT

If you need to add another key/value pair in your meta_query, you can simply just add another array.

$meta_query = [
    [
        'key'     => 'job_status',
        'value'   => 'open',
        'compare' => '!='
    ],
    [
        'key'     => 'another key',
        'value'   => 'another value',
        'compare' => '!='
    ]
];
$query->set( 'meta_query', $meta_query );
| improve this answer | |
  • So is this method of querying posts via a function the best way? – Pete Jan 23 '16 at 6:10
  • 1
    Yes, pre_get_posts is the correct and recommended way of altering the main query. You should avoid using any type of custom query (custom loop) to replace the main query with. You should take your time and work through this post in which I explained everything in detail ;-) – Pieter Goosen Jan 23 '16 at 6:22
  • And lastly, if I wanted to query the posts with more than 1 key/value would it just be a matter of adding the extra lines? – Pete Jan 23 '16 at 6:33
  • I have updated my answer. Enjoy ;-) – Pieter Goosen Jan 23 '16 at 6:38

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.