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 '';
print_r($args);
echo ' ';
}
$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 .= '';
$output .= '';
$output .= '' . get_field('victim_name') . ' ' . get_field('victim_lastname') . ' ';
$output .= '' . get_field('victim_date') . ' - ' . get_field('victim_location_city') . ', ' . get_field('victim_location_department') . ' ';
$output .= '';
$victim_perpetrators = get_field('victim_perpetrators', $post->ID);
if ($victim_perpetrators) {
$output .= ' ' . implode(', ', $victim_perpetrators) . ' ';
}
$output .= ' ';
$output .= '';
$output .= '';
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]
|