Nesha8x8 Nesha8x8 - 6 months ago 116
PHP Question

filter and sort wordpress get_posts with advanced custom fields

I have two custom fields created with Advanced Custom Fields.
One is type of checkbox (isEvent) and another is type of date (closing_date).
If I want to get all the posts that are events I would do something like this

<?php $args = array(
'posts_per_page' => 7,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => '',
'order' => '',
'include' => '',
'exclude' => '',
'meta_key' => 'is_event',
'meta_value' => 'a:1:{i:0;s:4:"true";}',
'post_type' => 'events',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => 0
);
$my_posts_array = get_posts( $args );


And this is working.
But If I would like to sort by closing_date which is the custom field of type date, in the ACF documentation for sorting is suggested that i should do something like this:

<?php $args = array(
'posts_per_page' => 7,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'meta_value_num',
'order' => 'asc',
'include' => '',
'exclude' => '',
'meta_key' => 'closing_date',
'meta_value' => '',
'post_type' => 'events',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => 0
);
$my_posts_array = get_posts( $args );


This is not working. Update - I managed to get this thing to work. I did not change anything, after few tests it worked....

Can someone give answer on any of these questions (or all) ?

1.What is the way to have two custom fields in wordpress and you want
to sort by one and filter by another ?
2. Is this possible in one call of get_posts or is there any other
native wp technique?
3. Can get_posts have meta_key, meta_value and meta_query in same
argument list for get_posts?

I also want to add also that I am using the WPML translation plugin with 2 languages en and fr.

Answer

The way to write the query is

$my_posts_array = get_posts( array(
        'post_type'         => 'events',
        'posts_per_page'    => '7',
        'offset'            => 0,
        'post_status'       => 'publish',
        'suppress_filters'  => 0,
        'meta_query'        => array(
            array(
                'key'       => 'is_event',
                'value'     => 'a:1:{i:0;s:4:"true";}',
                'compare'   => '='
            )
        ),
        'orderby'  => 'meta_value_num',
        'meta_key' => 'closing_date',
        'order'    => 'ASC',
        )
    );

2.Yes this is possible in one call try above code or if not woeking then try this.

$my_posts_array = get_posts( array(
    'post_type'         => 'events',
    'posts_per_page'    => '7',
    'offset'            => 0,
    'post_status'       => 'publish',
    'suppress_filters'  => 0,
    'meta_query'        => array(
        array(
            'key'       => 'is_event',
            'value'     => 'a:1:{i:0;s:4:"true";}',
            'compare'   => '='
        )
    ),
    )
);

// The Loop
foreach ($my_posts_array as $key => $value) {
    $allowed_posts[] = get_the_ID();
}


$sorted_array = get_posts( array(
    'post_type'         => 'events',
    'posts_per_page'    => '7',
    'offset'            => 0,
    'post_status'       => 'publish',
    'suppress_filters'  => 0 ,
    'post__in'          =>  $allowed_posts,
    'orderby'           => 'meta_value_num',
    'meta_key'          => 'closing_date',
    'order'             => 'ASC',
);

foreach ($sorted_array as $key => $value) {
    //do the stuff here
}