Home Wordpress Tricks Wordpress conditional meta query

Wordpress conditional meta query

by therichpost
2 comments
How to add custom meta title and meta description in Wordpress?

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

You may also like

2 comments

unkbown April 17, 2018 - 7:07 am

Very helpful code thank you

Reply
therichpost April 17, 2018 - 5:03 pm

thank you

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.