Support

Account

Home Forums ACF PRO WP_Query and sub-fields

Solved

WP_Query and sub-fields

  • Hi

    I am trying to loop through a custom post types’ sub field to display certain posts on the front end but I’m not getting any result at all after trying several different snippets of code. Could you please advise.

    I have a custom post type of ‘schedule’ which I can add new posts too, within the posts backend I have a repeater field called ‘schedule_time_slot’, which contains a sub field, schedule_date’. Which is a Date Picker field.

    My date picker field stores the date format as ‘d-m-Y’ which checked against my variable of $mon which is also ‘d-m’Y;

    EDIT: One thing I did notice is that when changing compare to: ‘compare’ => ‘!=’, all the posts display, but trying ‘LIKE’ and ‘=’ doesn’t show anything?

    I’d like to query my posts, to display any posts with a ‘schedule_date’ of Monday, then Tuesday, Wednesday and so on.
    I have my days of the week stored in variables such as:
    $mon; $tue; $wed and so on.

    Here is what I thought would work:
    Method 4: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    Here is my WP_Query:

    <?php
    function my_posts_where( $where ) {
      $where = str_replace("meta_key = 'schedule_time_slot_%", "meta_key LIKE 'schedule_time_slot_%", $where);
      return $where;
    }
    add_filter( 'posts_where', 'my_posts_where' );
    ?>
    
    <?php
    	$args = array(
    							'posts_per_page' => -1,
    							'post_type'      => 'schedule',
    							'meta_query'     => array(
    								array(
    									'key'     => 'schedule_time_slot_%_schedule_date',
    									'compare' => 'LIKE',
    									'value'   => $mon,
    								)
    							)
    	);
    ?>
    
    					<?php
    						$the_query = new WP_Query( $args );
    					?>
    					
    					<?php if( $the_query->have_posts() ): ?>
    						<ul>
    						<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    							<li>
    								<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    							</li>
    						<?php endwhile; ?>
    						</ul>
    					<?php endif; ?>
    					
    					<?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
  • Hi @huwrowlands

    Could you please tell me which version of ACF did you use? The date picker field in ACF PRO version saves the data in Ymd (yyyymmdd) format, so you need to reformat your $mon variable to Ymd. Please take a look at this page to learn how to convert it: http://stackoverflow.com/questions/2487921/convert-date-format-yyyy-mm-dd-dd-mm-yyyy.

    Thanks 🙂

  • I am using ACF PRO V5.3.9.
    Within the date picker field though, the Return Format is set to d-m-Y.
    Also, when echoing out the sub_field within the repeater loop, the date outputs as d-m-Y.

    I have now converted my $mon variable to the format suggested, Ymd, and it works. Awesome.
    So mental note made: always use Ymd (yyyymmdd) as my base date formats!

    Thanks very much James.

  • James; one thing I have just noticed though is that, if I set the date of the show to the Monday just passed and the Monday coming, both dates echo out.
    (As this example show may be ‘on air’ every Monday of every week)

    How would I edit my code so that it only displays the show for the Monday of ‘this week‘?

    // Get dates for the week, starting on a monday
    $monday    = strtotime('last monday', strtotime('tomorrow'));
    $tuesday   = strtotime('+1 days', $monday);
    $wednesday = strtotime('+2 days', $monday);
    $thursday  = strtotime('+3 days', $monday);
    $friday    = strtotime('+4 days', $monday);
    $saturday  = strtotime('+5 days', $monday);
    $sunday    = strtotime('+6 days', $monday);
    
    $mon = date( 'Ymd', $monday );
    etc...
    
    						<?php
    							function my_posts_where( $where ) {
    								$where = str_replace("meta_key = 'schedule_time_slot_%", "meta_key LIKE 'schedule_time_slot_%", $where);
    								return $where;
    							}
    							add_filter( 'posts_where', 'my_posts_where' );
    						?>
    
    						<?php
    							$args = array(
    								'posts_per_page' => -1,
    								'post_type'      => 'schedule',
    								'meta_key'       => 'schedule_time_slot_%_schedule_date',
    								'meta_query'     => array(
    									array(
    										'key'     => 'schedule_time_slot_%_schedule_date',
    										'compare' => '=',
    										'value'   => $mon,
    										'type'    => 'DATE',
    									)
    								)
    							);
    						?>

    Many thanks

  • Hi @huwrowlands

    That’s weird. Your query should search only the post in the current week. Could you please tell me how did you set the date? Did you use the repeater field? Also, could you please try this query:

    $args = array(
        'posts_per_page' => -1,
        'post_type'      => 'schedule',
        'meta_query'     => array(
            array(
                'key'     => 'schedule_time_slot_%_schedule_date',
                'compare' => '=',
                'value'   => $mon,
            )
        )
    );

    If that doesn’t work, could you please share the JSON export of your field group?

    Thanks 🙂

  • Sorry @acf-support / James, it’s working fine actually. Apologies.

    One thing I have noticed though and maybe good for another thread actually. But I also need to order the posts returned in time order. I have a custom field (within the same repeater) called ‘start_time’ and the posts need to be ordered by that… Any ideas how I modify my WP_Query for that?

    <?php
      $args = array(
        'posts_per_page' => -1,
        'post_type'      => 'schedule',
        'order'          => 'ASC',
        'orderby'        => 'meta_value_num',
        'meta_key'       => 'schedule_time_slot_%_schedule_date',
        'meta_query'     => array(
         array(
           'key'     => 'schedule_time_slot_%_schedule_date',
           'compare' => '=',
           'value'   => $mon,
           'type'    => 'DATE',
         )
        )
    );
    ?>

    Your help is much appreciated.

  • Hi @huwrowlands

    I’m afraid the ordering post by subfield is not possible. What you can do is creating a dummy fields that will hold the time field for the post. That way you can query the post normally. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/.

    If you still need to order by the subfield, I’m afraid you need to use the wpdb class instead. This page should give you more idea about it: https://codex.wordpress.org/Class_Reference/wpdb.

    I hope this helps 🙂

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

You must be logged in to reply to this topic.