Support

Account

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

  • 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 😀