Home Forums General Issues Create filter by year on archive page


Create filter by year on archive page

  • Hello!

    Firstly, thanks for the awesome plugin!

    I have two questions:

    1) How can I get all values from my acf.

    Now I using this

    global $wpdb;
    $years = $wpdb->get_results( 'SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key LIKE "year"', OBJECT );

    But I think it’s not a good tone.

    2) How can I create the filter by ‘year’ acf?

    I’m trying to follow this guide but without any luck.
    I’m getting 404 error when I’m going to /year?=2017.


  • The problem is that your url with ?year=2017 is conflicting with a build in rewrite rule for year posts in the blog.

    What you need to do is change the variable from year to something that does not conflict with a built in WP parameter.

  • @hube2 Thank you!

    It’s done.

    I used this ‘pre_get_posts’ function –

    add_action( 'pre_get_posts', 'my_pre_get_posts' );
    function my_pre_get_posts( $query ) {
    	if ( is_admin() ) {
    		return $query;
    	if ( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post' ) {
    		if( isset($_GET['press']) ) {
    			$query->set('meta_key', 'press');
    			$query->set('meta_value', $_GET['press']);
    	return $query;
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Create filter by year on archive page’ is closed to new replies.