phpwordpressloopsfieldadvanced-custom-fields

Wordpress Loop + Order by Custom Field


I have managed to order correctly by Custom field using the code below.

HOWEVER on my category pages where I want this, it lists all posts from all categories and not the category of the page in question. If I remove the code at the top it works fine. Please help, thanks.

   global $wp;

'posts_per_page' => '100',
'post_status'=>'publish',
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',

 );
 $new_query = $s_array;
 query_posts($new_query); 
?>


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

Solution

  • Since You're modifying the category page query, first you need to "global" it, then add arguments to the existing query. Note, I haven't tested this exact snippet, but hope it gets on on the right track.

    global $wp_query;
    
    $args = array_merge( $wp_query->query, array('posts_per_page' => '100',
    'post_status'=>'publish',
    'meta_key' => 'event_date',
    'orderby' => 'meta_value','order' => 'ASC'));
    
    $wp_query->query($args);