Support

Account

Home Forums General Issues Can't query on custom fields

Solved

Can't query on custom fields

  • I’ve followed the documentation and added custom datepicker fields to my theme through functions.php, because I want these fields to be a part of the theme. Now I want to query my posts based on these fields just like the example in the docs, but the posts don’t show up when I add the query to the arguments.

      if( function_exists('acf_add_local_field_group') ):
        acf_add_local_field_group(array (
          'key' => 'event_details',
          'title' => 'Event Details',
          'fields' => array (
            array (
              'key' => 'event_startdate',
              'label' => 'Start Date',
              'name' => 'start_date',
              'type' => 'date_picker',
              'required' => 1,
            ),
            array (
              'key' => 'event_enddate',
              'label' => 'End Date',
              'name' => 'end_date',
              'type' => 'date_picker',
              'required' => 1,
            ),
            array (
              'key' => 'event_featured',
              'label' => 'Featured Event',
              'name' => 'featured_event',
              'type' => 'true_false'
            )
          ),
        	'location' => array (
        		array (
        			array (
        				'param' => 'post_type',
        				'operator' => '==',
        				'value' => 'post',
        			),
        		),
        	),
        ));
    
      endif;

    This is where I create the fields in functions.php. event_startdate is the start date of an event, event_enddate is the end date and event_featured is just a boolean.

    This is my implementation of the query in the docs:

    <?php
            $today = date('Ymd');
    
            $args = array (
              'post_type'              => 'post',
            	'posts_per_page'         => '9',
            	'order'                  => 'ASC',
            	'orderby'                => 'id',
              'meta_query' => array(
            		array(
            	        'key'		=> 'event_startdate',
            	        'compare'	=> '>=',
            	        'value'		=> $today,
            	    ),
            	     array(
            	        'key'		=> 'event_enddate',
            	        'compare'	=> '<=',
            	        'value'		=> $today,
            	    )
                ),
            );
    
            $query = new WP_Query( $args );
    
            if ( $query->have_posts() ) {
            	while ( $query->have_posts() ) {
            		$query->the_post();
                            echo 'SUCCES';
            	}
            } else {
              echo 'FAIL';
            }
            wp_reset_postdata();
          ?>

    I want to show all the posts for that are currently “active”, so the current date is between the start and event date of the post. This results in a fail, unless I remove the meta_query.

    I use the same code for the query with event_featured, only the meta_query is different:

              'meta_query'             => array(
                  array(
                      'key' => 'field_event_featured',
                      'value' => '1',
                      'compare' => '=='
                  )
              )

    What am I doing wrong? I’ve looked everywhere the past day but couldn’t find anything.

  • Found the issue. Defining the fields manually in PHP was the issue. Create them through the admin interface and if you want to include them with your team, use the export tool to export to PHP.

  • It is possible to query customs fields created manually in php.
    You have to use the “full path” of the fieldname including the groupname.

    
    'meta_query' => array(
       array(
             'key' => 'event_details_event_startdate',
             'value' => $today,
             'compare' => '>='
      )
    )
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Can't query on custom fields’ is closed to new replies.