Support

Account

Home Forums Add-ons Repeater Field filter custom fields data by date

Solving

filter custom fields data by date

  • Hi, I need to filter some custom post by the current date, the code I pasted below works fine, but the only thing is that it does not show the data for all the post_types, only for the one named: ‘tonificar’, I don’t know why, I’ve checked the custom post types names and they are ok. If I remove from the array the ‘tonificar’ post_type, not data is shown, if I set post_type => any only the ‘tonificar’ data is displayed.
    Hope you can help me, thanks

    <?php 
    	$today= date('Ymd');
    	$args = array(
    		'post_type' => array(
    			'acuaticas',
    			'dirigidas',
    			'cuerpomente',
    			'servicios',
    			'infantil',
    			'tonificar'
    			),
    		'post_per_page' => 14
    
    		);
    
    	$the_query = new WP_Query ( $args ) ;
    
    	if( $the_query->have_posts() ) : 
    		while ( $the_query->have_posts() ) : $the_query->the_post();?>
    		
    		 <?php if( have_rows('schedule')) : while ( have_rows('schedule')) : the_row(); 
    				//if the repeater sub-field matches todays date ?>
    				 <li>
    					<?php if(get_sub_field('dia') == $today) { ?>
    						<a href="<?php the_permalink(); ?> ">
    							<span class="data-time"><?php the_sub_field('start'); ?></span>
    							<span class="data-name"><?php the_title(); ?></span>
    						</a>
    					<?php } ?>
    				</li>
    			<?php endwhile; 
    	
    			endif; ?>
    		<?php endwhile; ?>
    	
    	<?php endif; ?>
    <?php wp_reset_query();	?>
  • I don’t see anything wrong with your query. If the post types exist then is should be working, it’s a pretty simple query. My first step would be to see what posts it’s actually returning to see if I’m getting any of the other posts

    
    $args = array(
    		'post_type' => array(
    			'acuaticas',
    			'dirigidas',
    			'cuerpomente',
    			'servicios',
    			'infantil',
    			'tonificar'
    			),
    		'post_per_page' => 14
    
    		);
    
    	$the_query = new WP_Query ( $args ) ;
    
    echo '<pre>'; print_r($the_query->posts); echo '</pre>';
    
  • Hi, John Huebner, thank you for your reply, and yes is a simple query, but it didn’t work, don’t know why but it didn’t. What i did was to changed it a little bit, this way is almost the same but i add categories to every custom post and it worked.
    Is giving me another problems in another page that use a similar query, but with the code to print the post i think i will find a solution, so thank you for that!!

    <?php 
    	$today= date('Ymd');
    	$args_cat = array(
    	'include' => '11,12,13,14,15,17'
    	);
    
    	$categories = get_categories( $args_cat);
            foreach ($categories as $category) :
    		$args = array(
    		'post_type' => array( 
                          'acuaticas',
                          'dirigidas',
                          'tonificar',
    		      'cuerpomente',
    		      'infantil',
    		      'servicios'
    		),
    		'category__in' => $category->term_id,
    	);
    
            $the_query = new WP_Query ( $args ) ;
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘filter custom fields data by date’ is closed to new replies.