Support

Account

Home Forums General Issues Filter posts by just month and year values from Datepicker Custom field

Solving

Filter posts by just month and year values from Datepicker Custom field

  • I’m using the date-picker custom field for a post type called ‘study-tracker’. On the front-end I am trying to filter a series of ‘study-tracker’ search results by only the month and the year value of the date picker (while keeping the day value for other uses). Can someone point me in the right direction to achieve this?

    Thanks,
    Jon

  • Hi @jlin13

    Please review the documentation article below:
    http://www.advancedcustomfields.com/resources/how-to/filter-posts-by-custom-fields/

    There are some examples that show how to query the posts based on a custom field value (date picker value) between 2 values.

    To find all ‘January’ Posts, you can find all posts between 20140101 and 20140201

    Thanks
    E

  • Hi Elliot.

    What if I want do query ALL posts by month JANUARY from years ALL ~ 2014 ?

    I’m trying to create a birthday system that return all birthdays by current month or the next 10 birthdays.

    I can put the date_query if I use the wordpress post_date but this is not the case. How use the meta_query system for that?

    I created a datepicker field with default config.

  • @andremacola

    I achieved this in a meta_query by using REGEXP. I couldn’t find a way to do it with just dates and a range, because like you I wanted it to be year-agnostic.

    The date format by default is stored as YYYYMMDD. The following searches for a string like $$$$MM$$, where MM is your month. In other words, any 4 digits, followed by a specified 2-digit month, followed by any 2 digits.

    $filter_month = '09'; // show september only
    
    $args = array (
      'post_type'      => 'course',       // your custom post type
      'meta_key'       => 'course_date',  // your custom date field name
      'orderby'        => 'meta_value_num',
      'order'          => 'ASC',
      'meta_query' => array(
        array(
          'key'      => 'course_date',
          'compare'  => 'REGEXP',
          'value'    => '[0-9]{4}' . $filter_month . '[0-9]{2}',
        ),    
      )
    );
    
    $posts = get_posts($args);
    

    A little late, but hope this helps.

  • Hi @banjer

    I did not know about “REGEXP”, thks for the tip. But I did this with some another workaround.

    1 – First I created a simple text field for “ORDER” named “fm_mun_aniversario”
    2 – The birthdays is the normal wp post_date field
    3 – So I created a function to catch the month/day when save the post and put in the custom field with MMDD format:

    function save_mun_meta( $post_id ) {
    	$slug = 'municipios';
    	if ( $slug == $_POST['post_type'] ) {
    		$fm_mun_aniversario = get_the_time('md');
    		update_post_meta( $post_id, 'fm_mun_aniversario', $fm_mun_aniversario);
    	}
    }
    add_action( 'save_post', 'save_mun_meta' );

    4 – And the query:

    	$ordem = date('md');
    	$meta_query = array(
    		array(
    			'key' => 'fm_mun_aniversario',
    			'value' => $ordem,
    			'type' => 'NUMERIC',
    			'compare' => '>='
    		)
    	);
    	$proximos_args = array(
    		'paged' => $paged,
    		'post_type' =>'municipios',
    		'order' => ASC,
    		'orderby' => 'meta_value_num',
    		'meta_key' => 'fm_mun_aniversario',
    		'meta_query' => $meta_query,
    		'posts_per_page' => 6
    	);
            $proximos_items = new WP_Query( $proximos_args );
    
    	if ($proximos_items->have_posts()) :
    		while ( $proximos_items->have_posts() ) : $proximos_items->the_post();
    			get_template_part('templates/MunAniversario');
    		endwhile;
    	else:
    		echo "<p class='empty'>empty</p>";
    	endif;

    Worked for me 😀

  • Hi @andremacola

    That’s a good solution too, and probably more efficient than querying with a regex.

    I don’t see REGEXP in the WP codex, but it’s available. Looks like they added it within the past year, so perhaps it hasn’t made the docs yet. I’m running WP 3.9.2.

    Cheers

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Filter posts by just month and year values from Datepicker Custom field’ is closed to new replies.