1

How I can create filter which will filter posts by one custom field - year?

Field name: year

Field type: number

I need to setup some dropdown in page with existing values.

For example in 3 posts I'll enter some years(2017, 2016, 2015 etc.) and I need to display dropdown in page with 3 values - 2017, 2016, 2015.

I'm using this code:

add_action('pre_get_posts', 'my_pre_get_posts');

function my_pre_get_posts( $query ) {
if( is_admin() ) {
    return;
}

$year= $query->get('meta_query');
if( !empty($_GET['year']) ) {
    $lerge = array(
        'key'       => 'year',
        'value'     => '2017',
        'compare'   => 'LIKE',
    );
}

$query->set('meta_query', $year);
return;
}

And if I go to

?year=2017

I'm getting 404 page not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.