wordpress meta query key serialized array - php

So im having some problems getting this query working, im using a meta query to get values that have been stored as a serialized array. Im using the "LIKE" operator like so:
query_posts(array(
'post_type' => 'product',
's' => $keyword,
'meta_query' => array(
array(
'key' => 'raw_data',
'value' => '"'.$collection.'"',
'compare' => 'LIKE'
),
),
$taxQuery => array(
array(
'taxonomy' => 'product_category',
'field' => 'slug',
'terms' => array( $brandOne, $brandTwo, $brandThree )
)),
'posts_per_page' => '24',
'paged' => $paged,
'post_status'=>'publish' ,
'order'=>'ASC',
));
}
But What I need is target a specific key instead of getting the entire table of "raw_data" I need to get something like $raw_data["COLLECT"] and then query the value.
Anything you guys can think of would be great!

your question about how one can query on serialized data has already been discussed in this post, you may find answer there : https://wordpress.stackexchange.com/questions/16709/meta-query-with-meta-values-as-serialize-arrays

Related

How i can compare value in wordpress custom post type loop using taxonomy as key

I am trying to search posts by comparing the value that i am getting from URL with taxonomy of the post. But i couldn't get it right. Here is my code.
$vendita = $_GET['vendita'];
$the_query = new WP_Query(
array(
'post_type' => 'custom_post_type',
'taxonomy' => 'taxonomy_name',
// 'paged' => $paged,
'meta_query' => array(
'key' => 'taxonomy',
'value' => $vendita,
'compare' => 'LIKE'
)
)
);
Thanks in advance.
First, if you are going to take parameters, you must always sanitize them, like with $vendita = sanitize_text_field($_GET['vendita']);.
This is not tested, but I think what you are looking for is this WP_Query
$the_query = new WP_Query(
array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy_slug',
'field' => 'slug',
'terms' => $vendita,
),
),
)
);
Note the 'field' can be the term_id or the name instead of the slug, I do not know what $vendita is exactly.
Note tax_query is a nested array, it must be that way.
Note: You can replace array() with the shorter [].
Note: From docs: If you use the_post() with your query, you need to run wp_reset_postdata() afterwards to have template tags use the main query’s current post again.

Sorting query ASC giving wrong order

I am creating a simple Query that should query through a meta value containing a number. 1 should be first, 2 should be 2nd and 3 should be 3rd.
For some reason, it comes out like 1, 3, 2 in my query. What am I missing??
$args = array(
'post_type' => 'x-portfolio',
'posts_per_page' => $count,
'paged' => $paged,
'orderBy' => 'meta_value_num',
'meta_key' => 'liste_nr',
'order' => 'asc',
'tax_query' => array(
array(
'taxonomy' => 'portfolio-category',
'field' => 'term_id',
'terms' => $filters,
),
array(
'taxonomy' => 'portfolio-category',
'field' => 'name',
'terms' => 'Accessories',
'operator' => 'NOT IN'
)
)
);
I've run into this before when a CPT, plugin etc has done a custom query and they never reset and it can completely override yours.
Try dropping wp_reset_query() before it and make sure you are using it after.
https://developer.wordpress.org/reference/functions/wp_reset_query/

WordPress: search by Term, Post meta OR Post title together

we are writing a custom search function which searches for the posts by given Term OR Post Meta OR post_title
We have multiple terms and Post Meta values and We're looking for the solution to apply OR operation on few meta & term values.
bellow is the query that we have so far but ofcourse, is not working as we required
$search_keyword = $_GET['search_keyword'];
$args = array(
'post_type' => 'product',
'post_title' => $search_keyword,
'tax_query' => array(
array(
'taxonomy' => 'property-city',
'terms' => $search_keyword,
'field' => 'slug',
'operator' => 'LIKE'
),
array(
'taxonomy' => 'property-type',
'terms' => array('sale', 'rent'),
'field' => 'slug',
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => 'min_beds',
'value' => '2',
'compare' => '>=',
),
array(
'key' => 'min_baths',
'value' => '1',
'compare' => '>=',
),
array(
'key' => 'address',
'value' => $search_keyword,
'compare' => 'LIKE',
)
)
);
$the_query = new WP_Query($args);
$search_keyword contains the string value that needs to search. And only those posts should be selected that have a meta key matched with given $search_keyword OR matched with term OR matched with Post Title
also, keeping the other conditions as it is.
NOTE: relation b/w Taxonomy property-type and Post Meta min_beds/min_baths should be AND
I'm pretty sure this is not possible. You can either write custom SQL, or just do two or three queries and merge the results - that would probably be far easiest.

php query based on relational child elements for wordpress loop

I will try and explain this a simple as possible.
I have 3 post types that come into play here.
reports
events (motogp-2013, motogp-2014)
circuits
On my 'events' single.php to display the event information, I am trying to create a query to display related 'reports'.
And the only relation that the event content can have with a report is the circuit.
When I create a 'report', I have to assign an 'event' to it. But when I create a 'event' prior to a report, I have to assign a 'circuit' to it.
For example, it looks like this...
> 'report' - Second place for Pedrosa as black flag terminates race for Marquez (1023)
> 'event' - Australian Grand Prix (662)
> 'circuit' - Phillip Island (156)
So I am trying to query 'reports', which had 'events' at specific 'circuits'.
On my 'events' single.php, I do have the circuit id, but I don't know how to list the reports that which are at that circuit.
I can easily show related 'events' using this query
$related = new WP_Query(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
But this is not what I'm trying to achieve, I need to show related reports.
This reports_race_event meta key contains the id number of the 'event', and the 'event' contains the meta key event_calendar_circuit which contains the circuit id.
My question is how do I do this for 'reports', when the only meta_key I have is reports_race_event
I need to list 'reports' which we're held at a specific $circuit
If any one can help me that would be great thanks.
I've figured out how I can do this!
But I still need help please, I can list all related reports like this...
$related_reports = new WP_Query(array(
'post_type' => 'reports',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'reports_race_event',
'value' => $events_at_circuit,
'compare' => not sure,
'type' => not sure
)
)
));
But I need to create an array of 'event' id numbers and store it in this $events_at_circuit variable.
Buy using this query first...
global $circuit;
$related_events = get_posts(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
My question is now, how do I get the returned post id numbers from the $related_events query and store them as an array in $events_at_circuit
I eventually got there...
Here is the answer, I had to store the array numbers for the events that where at that circuit, then queried by meta_key value using an array variable. Then comparing using wordpress meta IN comparison. See full code below...
$events_at_circuit = null;
$related_events = get_posts(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
if( !empty( $related_events ) ){ foreach( $related_events as $related_event ) $events_at_circuit[] = $related_event->ID; }
$related_reports = new WP_Query(array(
'post_type' => 'reports',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'reports_race_event',
'value' => $events_at_circuit,
'compare' => 'IN',
'type' => 'NUMERIC'
)
)
));
if ( $related_reports->have_posts() ) :

Wordpress: WP_Query search criteria on 'post_name'

I'm using WP_Query (pretty standard). It all works great.
However, I have a particular modification to make, where, if the user enters the specific post name in the URL, the search will return only the post that matches that post_name value.
See my code below with a comment about the particular line not working.
<?php
$getPeople = array(
'post_type' => 'person',
'posts_per_page' => -1,
// I want this below to only return me the post with this specific value.
// This doesn't error, but doesn't work either.
// I know it seems counter-productive to a 'search' but this particular case requires it.
// This has a hard-coded value at the moment.
'post_name' => 'rebecca-atkinson',
'orderby' => 'meta_value',
'meta_key' => 'last-name',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'gender',
'value' => $theGender,
)
),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'accent',
'field' => 'slug',
'terms' => $theAccent,
'operator' => 'IN',
),
array(
'taxonomy' => 'style',
'field' => 'slug',
'terms' => $theStyle,
'operator' => 'IN',
),
array(
'taxonomy' => 'age',
'field' => 'slug',
'terms' => $theAge,
'operator' => 'IN',
),
)
);
$myposts = new WP_Query($getPeople);
?>
Your help would be greatly appreciated. If I could just see how to search on this specific 'slug' then that would be great.
Many thanks,
Michael.
Instead of
'post_name' => 'rebecca-atkinson',
use:
'name' => 'rebecca-atkinson',
In addition to my answer in the comments above, I thought I'd post it as an official answer too:
I have to use 'name' and NOT 'post_name'.
For example:
'name' => 'rebecca-atkinson'
Hope this helps someone in future.

Categories