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();
?>