WordPress conditional meta query

·

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

Comments

2 responses to “WordPress conditional meta query”

  1. unkbown Avatar
    unkbown

    Very helpful code thank you