Wordpress Tricks

WordPress conditional meta query

Wordpress conditional meta query

wordpress
293Views

Hello to all, welcome to therichpost.com. In this post, I will tell you, WordPress conditional meta query. I will tell you WordPress conditional meta query with multiple value with if condition. I used this query in my project and this is very helpful if we have multiple value but we need to use some of them.

Here is the code for WordPress conditional meta query:

<?php

$meta = array();

if (isset($_GET[‘location’][0]) && !empty($_GET[‘location’][0])) {
// restrict
$meta[] = array(
‘key’ => ‘location’,
‘value’ => $_GET[‘location’][0] ,
‘compare’ => ‘LIKE’);
}
if (isset($_GET[‘language’][0]) && !empty($_GET[‘language’][0])) {
$meta[] = array(
‘key’ => ‘language’,
‘value’ => $_GET[‘language’][0],
‘compare’ => ‘LIKE’);
}
if (isset($_GET[‘pr_area’][0]) && !empty($_GET[‘pr_area’][0])) {
$meta[] = array(
‘key’ => ‘pr_area’,
‘value’ => $_GET[‘pr_area’][0],
‘compare’ => ‘LIKE’);
}
if (isset($_GET[‘keywords’][0]) && !empty($_GET[‘keywords’][0])) {
$meta[] = array(
‘key’ => ‘keywords’,
‘value’ => $_GET[‘keywords’][0],
‘compare’ => ‘LIKE’);
}

$args = array( ‘post_type’ => ‘people’, ‘orderby’ => ‘title’,’order’ => ASC, ‘posts_per_page’ => -1,
‘meta_query’ => array(
‘relation’ => ‘OR’, $meta));
$loop = new WP_Query( $args );

There are so many tricks in wordpress and i will let you know all. Please do comment if you any query related to this post. Thank you. Therichpost.com

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

2 Comments

Leave a Reply

Positive SSL