Support

Account

Home Forums General Issues If get_fields is empty not working

Solving

If get_fields is empty not working

  • Hey guys,

    I’m struggling with a piece of code that is easy but I don’t get my error.

    I’m using this:
    ` $query_array = array(
    ‘post_status’ => ‘publish’,
    ‘post_type’ => ‘show-item’,
    ‘posts_per_page’ => $params[‘number_of_items’],
    ‘orderby’ => $params[‘orderby’],
    ‘order’ => $params[‘order’]
    );`

    And it’s working perfectly but I want to add a condition if a ACF field is not empty so i’m adding this piece of code right after the one above

    `if (get_field(‘date_de_debut’)) {
    $query_array[‘meta_key’] = ‘date_de_debut’;
    $query_array[‘orderby’] = ‘meta_value’;
    }`

    It looks like the get_field does not return anything. I tried a lot of method like

    $value = get_field('date_de_debut');
    if (!empty($value)) {
    ...
    }

    But again it looks like the field does not return anything. If I remove the ‘if’ statement the order works well accoridng to my meta value…

    Thanks guys!

  • You’re only setting the order. You also need to use “meta_value” and “meta_compare” settings https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

  • It’s working well with the IF condition so this is not the problem 🙂 The problem is that the variable is always empty even if I ise get_field() or the_field() 🙂

    If I use this code like this (without the if) it’s ordering like a charm !

    $query_array = array(
    ‘post_status’ => ‘publish’,
    ‘post_type’ => ‘show-item’,
    ‘posts_per_page’ => $params[‘number_of_items’],
    ‘orderby’ => $params[‘orderby’],
    ‘order’ => $params[‘order’]
    );
    $query_array[‘meta_key’] = ‘date_de_debut’;
    $query_array[‘orderby’] = ‘meta_value’;
  • That is because your query is not eliminating posts that don’t have a value

    
    $query_array['meta_value'] = '';
    $query_array['meta_compare'] = '!=';
    

    Other than this I don’t see an issue unless ACF is unable to figure out the post ID. Are you using the global $post variable for your loop? If not then you need to supply the correct post ID to get the value from in the second argument to the_field() or get_field().

  • They all have a value, this snippet replace the existing value

    if (get_field(‘date_de_debut’)) {
    $query_array[‘meta_key’] = ‘date_de_debut’;
    $query_array[‘orderby’] = ‘meta_value’;
    }

    I think this has to be with the post ID. But why is it working if I use this:
    $query_array = array(
    ‘post_status’ => ‘publish’,
    ‘post_type’ => ‘show-item’,
    ‘posts_per_page’ => $params[‘number_of_items’],
    ‘orderby’ => ‘meta_value’,
    ‘meta_key’ => ‘date_de_debut’,
    ‘order’ => $params[‘order’]
    );

  • Are you sure that this has the right values?

    
    
    ‘orderby’ => $params[‘orderby’],
    ‘order’ => $params[‘order’]
    
  • Yeah ! It’s working well with both code 🙂
    The only problem is here
    $value = get_field(‘date_de_debut’);

    I think that $value is always empty.

    An other example if I use this:

    $value = get_field('date_de_debut');
    if (empty($value)) {
    $query_array[‘meta_key’] = ‘date_de_debut’;
    $query_array[‘orderby’] = ‘meta_value’;$
    }

    The order change and works well according to the meta_key ‘date_de_debut’

  • Please post the code for your loop over the query results

  • It comes from a theme that we bought 🙂

    public function render($atts, $content = null) {
            $args = array(
    	        'type'                      => 'gallery',
    	        'number_of_columns'         => '3',
                'space_between_items'       => 'normal',
    	        'number_of_items'           => '-1',
                'category'                  => '',
                'selected_shows'            => '',
    	        'title_tag'                 => 'h5',
    	        'image_proportions'         => 'full',
    	        'enable_fixed_proportions'  => 'no',
    	        'show_category'             => 'no',
    	        'show_date_range'           => 'no',
                'orderby'                   => 'date',
                'order'                     => 'ASC',
    	        'show_single_layout'        => 'info-bellow',
    	        'show_slider'               => 'no',
    	        'slider_navigation'	        => 'no',
    	        'slider_pagination'	        => 'no',
    	        'pagination_type'          => 'standard',
    	        'load_more_top_margin'     => '',
    	        'slider_autoplay'       => 'no'
            );
    		$params = shortcode_atts($args, $atts);
    	
    	    /***
    	     * @params query_results
    	     * @params holder_data
    	     * @params holder_classes
    	     */
    		$additional_params = array();
    	    
    		$query_array = $this->getQueryArray($params);
    		$query_results = new \WP_Query($query_array);
    		
    		
    	    $additional_params['query_results'] = $query_results;
    	    $additional_params['holder_data']          = musea_elated_get_holder_data_for_cpt( $params, $additional_params );
    
    	    $additional_params['holder_classes'] = $this->getHolderClasses($params, $args);
    	    $additional_params['inner_classes']  = $this->getInnerClasses($params);
    	    $additional_params['data_attrs']     = $this->getDataAttribute($params);
    	
    	    $params['this_object'] = $this;
    	    
    	    $html = musea_shows_get_cpt_shortcode_module_template_part('shows', 'show-list', 'show-holder', $params['type'], $params, $additional_params);
    	    
            return $html;
    	}
    
    	/**
        * Generates show list query attribute array
        *
        * @param $params
        *
        * @return array
        */
    	public function getQueryArray($params){
    		$query_array = array(
    			'post_status'    => 'publish',
    			'post_type'      => 'show-item',
    			'posts_per_page' => $params['number_of_items'],
    			'orderby'        => $params['orderby'],
    			'order'          => $params['order']
    		);
    		
    		$field = get_field('date_de_debut');
    		if (!empty ($field)) {
    			$query_array['meta_key'] = 'date_de_debut';
    			$query_array['orderby'] = 'meta_value';
    		}
    
    		if(!empty($params['category'])){
    			$query_array['show-category'] = $params['category'];
    		}
    
    		$show_ids = null;
    		if (!empty($params['selected_shows'])) {
                $show_ids = explode(',', $params['selected_shows']);
    			$query_array['post__in'] = $show_ids;
    		}
    		
    		if ( ! empty( $params['next_page'] ) ) {
    			$query_array['paged'] = $params['next_page'];
    		} else {
    			$query_array['paged'] = 1;
    		}
    		
    		return $query_array;
    	}
    	
    	public function getLoadMoreStyles( $params ) {
    		$styles = array();
    		
    		if ( ! empty( $params['load_more_top_margin'] ) ) {
    			$margin = $params['load_more_top_margin'];
    			
    			if ( musea_elated_string_ends_with( $margin, '%' ) || musea_elated_string_ends_with( $margin, 'px' ) ) {
    				$styles[] = 'margin-top: ' . $margin;
    			} else {
    				$styles[] = 'margin-top: ' . musea_elated_filter_px( $margin ) . 'px';
    			}
    		}
    		
    		return implode( ';', $styles );
    	}
  • I’m not familiar with this code, but I can tell you that this is not where you would output anything. It would be in whatever template part the function musea_shows_get_cpt_shortcode_module_template_part is loading.

  • yeah this is not the output but it takes tu arguments here:

    public function getQueryArray($params){
    		$query_array = array(
    			'post_status'    => 'publish',
    			'post_type'      => 'show-item',
    			'posts_per_page' => $params['number_of_items'],
    			'orderby'        => $params['orderby'],
    			'order'          => $params['order']
    		);
    		
    		$field = get_field('date_de_debut');
    		if (!empty ($field)) {
    			$query_array['meta_key'] = 'date_de_debut';
    			$query_array['orderby'] = 'meta_value';
    }

    So there is probably a solution there !

  • $query_array = array(
       'post_status'    => 'publish',
       'post_type'      => 'show-item',
       'posts_per_page' => $params['number_of_items'],
       'meta_key'	    => 'date_de_debut',
       'orderby'	    => 'meta_value',
       'order'          => $params['order']
    );

    This code works well. The only problem is that the order posts does not match the criteria so it returns: no post match your criteria.

    I just need to change the orderby and the meta_key if get_field(‘date_de_debut) is not empty

  • Oh yeah sorry this is the real query

    <?php
    
    			if($query_results->have_posts()):
    				while ( $query_results->have_posts() ) : $query_results->the_post();
    					$single_params = array();
    					$single_params['title_tag'] = $title_tag;
    					$single_params['show_single_layout'] = $show_single_layout;
    					$single_params['show_id'] = get_the_ID();
    					$single_params['article_classes'] = $this_object->getArticleClasses( $params );
    					$single_params['show_category'] = $show_category;
    					$single_params['show_date_range'] = $show_date_range;
    					$single_params['image_proportions'] = $image_proportions;
    
    					echo musea_elated_execute_shortcode('eltdf_show_single', $single_params);
    				endwhile;
    			else:
    				esc_html_e( 'Sorry, no posts matched your criteria.', 'musea-shows' );
    			endif;
    		
    			wp_reset_postdata();
    		?>
  • I Tried to change the last query with this one

    <?php
    			// An array of arguments
    			$args = array(
    				'post_status'    => 'publish',
    				'post_type'      => 'show-item',
    				'posts_per_page' => $params['number_of_items'],
    				'orderby'		 => $params['orderby'],
    				'order'          => $params['order'],
    				'meta_query' => array( // WordPress has all the results, now, return only the events after today's date
    	            array(
    	                'key' => 'date_de_debut', // Check the start date field
    	                'value' => date("Y-m-d"), // Set today's date (note the similar format)
    	                'compare' => '>=', // Return the ones greater than today's date
    	                'type' => 'DATE' // Let WordPress know we're working with date
    	                )
    	            ),
    			);
    			$field = get_field('date_de_debut');
    			if (!empty ($field)) {
    				$args['meta_key'] = 'date_de_debut';
    				$args['orderby'] = 'meta_value';
    			}
    			// The Query
    			$the_query = new WP_Query( $args );
    			
    			// The Loop
    			if ( $the_query->have_posts() ) {
    			
    			while ( $the_query->have_posts() ) : $the_query->the_post(); 
    			    $single_params = array();
    					$single_params['title_tag'] = $title_tag;
    					$single_params['show_single_layout'] = $show_single_layout;
    					$single_params['show_id'] = get_the_ID();
    					$single_params['article_classes'] = $this_object->getArticleClasses( $params );
    					$single_params['show_category'] = $show_category;
    					$single_params['show_date_range'] = $show_date_range;
    					$single_params['image_proportions'] = $image_proportions;
    
    					echo musea_elated_execute_shortcode('eltdf_show_single', $single_params);
    			endwhile;
    			
    			} else {
    			    esc_html_e( 'Sorry, no posts matched your criteria.', 'musea-shows' );
    			}
    			/* Restore original Post Data */
    			wp_reset_postdata();
    		?>

    Butsame problem. Any idea?

  • My suggestion is to contact the theme author and ask them how you can alter the query to add additional arguments for custom fields and how to show custom fields.

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

The topic ‘If get_fields is empty not working’ is closed to new replies.