0

I currently have the following code where I filter by the page template;

$pages = get_pages( array(
‘post_type’ => ‘page’,
‘meta_key’ => ‘_wp_page_template’,
‘meta_value’ => ‘unit.php’,
‘hierarchical’ => 0
) );

I would also like to sort by an ACF field so would need to add something like;

‘meta_key’  => ‘status’,
‘orderby’   => ‘meta_value’,
‘order’ => ‘DESC’

Is it possible to get both meta_key parts to work together?

  • 1
    Keep in mind that this query will be hideously slow/expensive. Your status post meta should really have been implemented as a custom taxonomy – Tom J Nowell Mar 7 '18 at 17:21
  • 1
    Also, try to avoid the helper functions such as get_pages and stick to WP_Query, or if you must, get_posts, you can set the post_type to page – Tom J Nowell Mar 7 '18 at 17:21
0

I'm not sure if you can do it with get_pages(), but you should be able to do something like this with get_posts():

$args = array(
    'post_type'  => 'page',
    'meta_query' => array(
        'template_clause' => array(
            'key'     => '_wp_page_template',
            'value'   => 'unit.php',
            'compare' => '=', 
        ),
        'status_clause' => array(
            'key'     => 'status',
            'compare' => 'EXISTS',
        ),
        'relation' => 'AND',
    ),
    'orderby' => 'status_clause',
    'order'   => 'DESC',
);
$pages = get_posts( $args );

// Better option, per Tom J Nowell's comment on the question
$page_query = new WP_Query( $args );

I haven't tested this code, but I think it should do what you're looking for.

References

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.