This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Compare two custom fields before displaying them

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Compare two custom fields before displaying them

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #55431
    YansYans
    Participant

    Hi,
    I am using ASP to list and search events that come from the EventOn plugin.

    Each event has start date and end date.

    I configured ASP to show the start and end dates in the search results and using the following code (based on code you sent me) to convert timestamp to normal date:

    /**
     * Converting event start and end dates from UNIX timestamp to human date
     */
    add_filter('asp_cpt_advanced_field_value', function($value, $field_name){
    	if ( $field_name == 'evcal_srow' ) {
    		return date_i18n( "F j, Y", $value);
    	} elseif ( $field_name == 'evcal_erow' ) {
    		return  date_i18n( "F j, Y", $value);
    	} else {
    		return $value;
    	}
    }, 10,2);

    The problem is that some events have the same start and end date. With the current configuration, ASP displays both start and end dates.

    Is there a way to compare evcal_srow and evcal_erow and if they are the same, not display evcal_erow (the end date)?

    Thanks!

    #55432
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    Surely!

    The way I would try to do this is to check the start date and compare when the field name is “ecval_erow” and then return and empty string “”, that basically unsets the value and the field becomes empty:

    add_filter('asp_cpt_advanced_field_value', function($value, $field_name, $result){
    	if ( $field_name === 'evcal_srow' ) {
    		return date_i18n( "F j, Y", $value);
    	} elseif ( $field_name === 'evcal_erow' ) {
    		$start_date = get_post_meta($result->id, 'evcal_srow', true);
    		if ( $start_date == $value ) {
    			return '';
    		}
    		return  date_i18n( "F j, Y", $value);
    	} else {
    		return $value;
    	}
    }, 10,2);

    I’m not sure this is the final code, you may have to test it first.

    Then if you use the conditional square brackets to wrap the field, then you can hide the whole part with text and everything when the “evcal_erow” is returned empty (when it’s the same as the start date):

    #55446
    YansYans
    Participant

    Hi,

    Thank you very much for the quick reply!

    The code worked (had to change “10,2” to “10,3”).

    This ticket can be closed.

    #55455
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.