Support

Account

Home Forums Front-end Issues pre_get_posts – organising CPT posts with Datepicker field

Solving

pre_get_posts – organising CPT posts with Datepicker field

  • Hi there,

    I’ve created a custom post type, and I would like its archive page to display posts organised by date (using the ACF Datepicker field, not the date the post was created).

    I would like to achieve this with a custom plugin, as I want the posts to remain organised by date even when the theme is changed.

    I don’t have tons of experience with this so the only solution I know of is to use pre_get_posts. This is what I have in my custom plugin:

    function mycustomposttype_pre_get_posts( $query ) {
    	
    	if( is_admin() ) {
    		
    		return $query;
    		
    	}
    	
    	if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'mycustomposttype' ) {
    		
    		$query->set('orderby', 'meta_value');	
    		$query->set('meta_key', 'date');	 
    		$query->set('order', 'DESC'); 
    		
    	}
    
    	return $query;
    
    }
    
    add_action('pre_get_posts', 'mycustomposttype_pre_get_posts');

    Currently, the archive page for the custom post type is returning ‘Posts Not Found’. Do you know why this might be? Is there another approach I should be taking with this? I feel organising posts with the Datepicker field should be fairly straightforward, but I must be missing something.

    Thank you in advance for any help you can provide!

  • It’s may be just a matter of specifying the post type in the Query.

    It sounds like you know this, but remember… the below code naturally has values that need to be replaced:

    a) ‘my_custom_post_type’ = the ‘slug’ of your post type (3 places… plus the function name twice if desired)
    b) ‘date’ = the ‘name’ of your ACF Date field (1 place)

    
    <?php
    function mycustomposttype_pre_get_posts() {
      if ( is_admin() ) { return $query; }
      if ( is_post_type_archive( 'my_custom_post_type' )  && !empty( $query->query['post_type']  == 'my_custom_post_type' ) {
        $query->set( 'post_type', 'my_custom_post_type' );
        $query->set( 'meta_key', 'date' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'DESC' );
      }
      return $query;
    }
    add_action('pre_get_posts','mycustomposttype_pre_get_posts');
    ?>
    

    You may need to adjust ‘orderby’. I got ‘num’ from the ACF docs about querying date fields.

  • Hi Keith,

    Thank you for your reply and for helping me out – I really appreciate it

    Unfortunately the syntax within your suggested code is throwing errors for me, and I’m not sure why – do you perhaps have any ideas?

    Thank you!

  • Hi Keith, I’m getting ‘Parse error: syntax error’ for the curly bracket at the end of this line:

    if ( is_post_type_archive( 'my_custom_post_type' ) && !empty( $query->query['post_type'] == 'my_custom_post_type' ) {

    Nothing I have tried seems to fix the issue. Thank you!

  • ahhh… silly mistake on my part… the “if” is missing the closing bracket… sorry about that.

    if ( is_post_type_archive( 'my_custom_post_type' ) && !empty( $query->query['post_type'] == 'my_custom_post_type' ) ) {

  • No problem Keith, thank you very much for looking into that. Unfortunately this solution doesn’t seem to have worked – it hasn’t changed the sorting of the posts at all, so the problem must be elsewhere.

    Again, I do appreciate your help! 🙂

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

The topic ‘pre_get_posts – organising CPT posts with Datepicker field’ is closed to new replies.