Horje
Query Parameters Errors - Wordpress Solution
I want to perform a search where I can get results from a key query, the query is already working if I use only 2 parameters that shares the same "compare" setting, https://ibb.co/0c4rwkh but if I add another parameter then I get this error. https://ibb.co/68wZn5Y this is my code:

add_shortcode('victimas', 'exp_post_slider_shortcode_victimas');

function exp_post_slider_shortcode_victimas($atts)
{
    $a = shortcode_atts(array(
    'post_type' => 'victima',
    'posts_per_page' => '10',
), $atts);

    $output = '';
    $meta_query = array('relation' => 'OR');
    $args = array(
        'post_type' => 'victima',
        'posts_per_page' => $a['posts_per_page'],
	);

    if (isset($_GET['victim_age'])) {
        $meta_query[] = array(
            'key' => 'victim_age',
            'value' => $_GET['victim_age'],
            'compare'      => 'LIKE'
        );
    }

    // allow the url to alter the query
    if (isset($_GET['victim_name'])) {
        $meta_query[] = array(
            'key' => 'victim_name',
            'value' => $_GET['victim_name'],
            'compare' => 'REGEXP',
        );
    }

	// allow the url to alter the query
    if (isset($_GET['victim_lastname'])) {
        $meta_query[] = array(
            'key' => 'victim_lastname',
            'value' => $_GET['victim_lastname'],
            'compare' => 'REGEXP',
        );
    }

    // allow the url to alter the query
    if (isset($_GET['victim_gender'])) {
        $meta_query[] = array(
            'key' => 'victim_gender',
            'value' => $_GET['victim_gender'],
        );
    }

	// allow the url to alter the query
	if (isset($_GET['victim_date_one'])) {
		$meta_query[] = array(
			'key' => 'victim_date',
			'value' => $_GET['victim_date'],
		);
	}
	// allow the url to alter the query
	if (isset($_GET['victim_date_two'])) {
		$meta_query[] = array(
			'key' => 'victim_date',
			'value' => $_GET['victim_date'],
		);
	}
    
	if (!empty($meta_query)) {
        $args['meta_query'] = $meta_query;
    }

    if ($_GET['test']) {
        echo '<pre>';
        print_r($args);
        echo '</pre>';
    }
    $post_slider = new WP_Query($args);

    if ($post_slider->have_posts()) {
        // The Loop
     
        while ($post_slider->have_posts()):
        
        $post_slider->the_post();

        $feat_image_url = get_field('victim_photo', $post->ID);
        $output .= '<a rel="nofollow" href="' . get_permalink() . '" class="result-victim">';

        $output .= '<div class="result-victim-img" style="background-image:url('.$feat_image_url.'); background-size:cover; background-repeat:no-repeat;"></div>';
        $output .= '<p>' . get_field('victim_name') . ' ' . get_field('victim_lastname') . '</p>';
        $output .= '<p>' . get_field('victim_date') . ' - ' . get_field('victim_location_city') . ', ' . get_field('victim_location_department') . '</p>';
        $output .= '<div>';
        $victim_perpetrators = get_field('victim_perpetrators', $post->ID);
        if ($victim_perpetrators) {
            $output .= '<p class="result-item-multiple">' . implode(', ', $victim_perpetrators) . '</p>';
        }
        $output .= '</div>';
        $output .= '<div></div>';

        $output .= '</a>';
    
        endwhile;

        wp_reset_postdata();
    } else {

    }

    return $output;
}


Solution - 1

Can you paste url here having victim name and last name? In second image name and last name are blank can you try adding some value? Please print query with below code global $wpdb; // Print last SQL query string echo $wpdb->last_query;


Solution - 2

Is it the same third parameter which produces error or any of them?


Solution - 3

I think you need a regular expression when doing the comparison if you add the value "REGEXP" to the "compare" field. Try changing the value to "LIKE" or add your regular expression.

// allow the url to alter the query
if (isset($_GET['victim_name'])) {
$meta_query[] = array(
'key' => 'victim_name',
'value' => $_GET['victim_name'],
'compare' => 'REGEXP',
);
}

// allow the url to alter the query
if (isset($_GET['victim_lastname'])) {
$meta_query[] = array(
'key' => 'victim_lastname',
'value' => $_GET['victim_lastname'],
'compare' => 'REGEXP',
);
}


Solution - 4

Hello Alvaro, Could you add the url here that having the victim name and last name? You may use LIKE or REGEXP for the query If it isn't working you can send me a temporary login to my email [email protected]





Wordpress

Related
I need to speed up my wordpress-site - Wordpress Solution I need to speed up my wordpress-site - Wordpress Solution
I want to create a search form that will only search a custom post typ - Wordpress Solution I want to create a search form that will only search a custom post typ - Wordpress Solution
ACF: How to programmatically Update Flexible Content Field? - Wordpress Solution ACF: How to programmatically Update Flexible Content Field? - Wordpress Solution
Wordpress posts per hour - Wordpress Solution Wordpress posts per hour - Wordpress Solution
Website recently received a Website recently received a

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
34