Categories

Monday, December 23, 2024
#919814419350 therichposts@gmail.com
Wordpress Tricks

WordPress conditional meta query

How to add custom meta title and meta description in Wordpress?

Wordpress conditional meta query

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

2 Comments

Leave a Reply

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