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 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;
}
Bob answers:
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;
Bob comments:
Please try setting php condition like below.
I think it's taking name and lastname value as blank so "isset" is only checking wether it's set or not.
it's set but blank.
if(isset($_GET["victim_name"]) && $_GET["victim_name"] != ""){
$meta_query[] = array(
'key' => 'victim_name',
'value' => $_GET['victim_name'],
'compare' => 'REGEXP',
);
}
Bob comments:
You can also try !empty($_GET["victim_name"] with isset
Bob comments:
other than that regex needs some regular expression see examples below
'meta_query' => array(
array(
'key' => 'email_address',
'value' => '^hello@',
'compare' => 'REGEXP',
)
)
'meta_query' => array(
array(
'key' => 'custom_fields',
'value' => 'foo[(][0-9][)]', // with regex stuff
'compare' => 'REGEXP',
),
),
Cesar Contreras answers:
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',
);
}
Mohamed Ahmed answers:
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]