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

Reply To: Conditional brackets – either/or possible?

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Conditional brackets – either/or possible? Reply To: Conditional brackets – either/or possible?

#42350
Ernest MarcinkoErnest Marcinko
Keymaster

Hi,

Well, the code only handles a simple case where the whole content field is replaced with the custom field values – regarless what the advanced title/content fields hold.

To make a replacement for the content field this has to be modified a bit. First of all for the advanced title/content field we will have to add a new “pseudo” variable, which is not regognized, and the custom code will deal with it and replace it with the output of the field logic, for that let’s use the made up variable name $$$field_values$$$.
Wherever you will place this variable in the advanced content field it is going to be replaced by the custom code below. So you can do something like:

{descriptionfield} $$$field_values$$$

Then the custom code (don’t forget to replace the field1_key and field12_key variable values with the field keys):

add_filter( 'asp_results', 'asp_my_custom_field_code', 10, 1 );
function asp_my_custom_field_code( $results ) {
    // Field names
    $field1_key = 'field_1';
    $field2_key = 'field_2';

    // --- DO NOT CHANGE ANYTHING BELOW ---
    foreach ($results as $k=>&$r) {
		$replacement = '';
		// ACF support
        if ( function_exists('get_field') ) {
            $field1 = get_field( $field1_key, $r->id, true ); 
            $field2 = get_field( $field2_key, $r->id, true ); 
        } else {
            $field1 = get_post_meta( $r->id, $field1_key, true );
            $field2 = get_post_meta( $r->id, $field2_key, true );
		}

        // Change only, if the meta is specified
        if ( empty($field1) ) {
			if ( !empty($field2) ) {
				$replacement = $field2;
			}
        } else {
			$replacement = $field1;
		}
		
		$r->content = str_replace('$$$field_values$$$', $replacement, $r->content);
    }

    return $results;
}