This is the first time am writing anything as such. I have an array that has to search serialized value in the database and return its matching results. Below is my current code that am still working on.
//This is my array
$country = array(128, 191, 158);
//This is my args passed to
$args = array(
'post_parent' => 85,
'posts_per_page' => 10,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'desc',
'post_status' => 'publish',
's' => $keyword? $keyword: NULL,
'meta_query' => array(
'relation' => 'AND',
$country? array('key' => 'country', 'value' => $country, 'compare' => 'IN'): NULL,
$related? array('key' => 'related', 'value' => $related, 'compare' => '='): NULL
)
);
query_posts( $args );
//Example of postmeta value in the database
$meta = 'a:6:{i:0;s:2:"20";i:1;s:2:"25";i:2;s:2:"128";i:3;s:2:"191";i:4;s:2:"23";i:5;s:2:"24";}';
Any help or directing me to the right resource will be appreciated.
Related
Why does this return all posts;
$partner = get_query_var('partner');
echo "Partner ID: ". $partner; // Echoes correctly
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'partner',
'meta_value' => array($partner), // also $partner (without array)
'compare' => 'LIKE', // Also 'IN'
),
);
$partner_query = new WP_Query($args);
the ACF field 'partner' is an array, the query variable is a string (obv)
The correct usage of meta_query is like this:
The args shouldn't be meta_key if you're using meta_query - they should be key, value, etc.
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner',
'value' => array( $partner ),
'compare' => 'IN',
),
),
);
Try like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'partner',
'meta_value' => array($partner), // also $partner (without array)
'compare' => '=', // Also 'IN'
),
);
$partner_query = new WP_Query($args);
For anyone who gets stuck in this rabbit hole.
My query parameter is a string, ACF stores the IDs as a string if there's one and an array if there's more than one.
This form of the arguments returns the correct results. (many thanks to Howard E and IronCanTaco)
$partner = get_query_var('partner'); // string
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner', // Could be string or array
'value' => $partner,
'compare' => 'LIKE',
),
),
);
i want to order posts by meta query, if featured & verified show first then verified and then normal without those meta keys
i tried this code its showing what i need but the order is wrong.
<?php
$sports = get_field('sport', $post_id);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'meta_key' => 'sport',
'meta_value' => $sports,
'post__not_in' => array ($post->ID),
'meta_query' => array(
'relation' => 'OR',
'featuredmet' => array(
'key' => 'featured',
'compare' => 'EXISTS',
),
'verified_athletemet' => array(
'key' => 'verified_athlete',
'compare' => 'EXISTS',
),
'commitedmet' => array(
'key' => 'commited',
'compare' => 'NOT EXISTS',
),
),
'orderby' => array(
'featuredmet' => 'DESC',
'verified_athletemet' => 'DESC'
)
);
//the query
$relatedPosts = new WP_Query( $args );
if anyone can help me with this super fast that would be great!
I want to query the posts that have been set as "featured" and order them by their "priority" field at the same time.
featured field is a true/false type and priority field is a number, and they are created by ACF plugin.
here is my code, but it's not working...
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num date',
'meta_key' => 'priority',
'meta_query' => array(
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
try this :
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num',
'order'=>'DESC',
'meta_key' => 'priority',
'meta_query' => array(
relation=>'AND',
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
array(
'key' => 'priority',
'value' => array(1,6), //YOUR VALUES
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
),
);
$query = new WP_Query( $args );
I want to build a dynamic set of arguments for a wp_query but have had real problems doing this. Take the following example code (which does work)...
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Primary',
'compare' => 'LIKE'
),
array(
'key' => 'years',
'value' => 'Secondary',
'compare' => 'LIKE'
)
)
);
// get results
$the_query = new WP_Query( $args );
The idea is that depending on the results of user input in a form, the number of clauses in my meta_query value would alter. In the code above there are two options, but maybe depending on input there are 3 or another time 5.
I tried compiling these inner array elements externally of $args. Assume in following code that $inputArray is a single dimensional array of string elements. The test looked like:
$inputArray=array();
if (is_array($yearsArray)){
foreach( $yearsArray as $year ) {
$inputArray[]=array('key' => 'years','value' => $year,'compare' => 'LIKE');
}
}
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
'relation' => 'OR',
$inputArray
)
);
But the query just runs as if no meta_query queries had been applied.
I also tried using the function wp_parse_args to try and merge multiple meta_queries, however it seems that I'm overwriting the value and only the last one is ever used. This looked like this:
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills'
);
$args2 = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Secondary',
'compare' => 'LIKE'
)
)
);
$args3 = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Primary',
'compare' => 'LIKE'
)
)
);
$args=wp_parse_args($args,$args2,$args3);
So as you can see I've tried a few different methods but none are working. Can anyone assist?
I fixed this by discovering that you can use IN as a compare clause. $yearsArray was just a single dimensional array of string values.
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
array(
'key' => 'years',
'value' => $yearsArray,
'compare' => 'IN'
)
)
);
This is my first question on here; I have hunted around for an answer but if it is out there already apologies.
I am using wp_query() to fetch both 'posts' and a custom post type called 'reviews'. However the custom post type has a sub type and I need to filter out all but one of these sub types just for reviews in the same query!
This is where I got to:
$type = array( 'post', 'review' );
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array( // I need to filter the review post type further by subtype
array(
'key' => 'subtype',
'value' => 'review'
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);
Obviously, when I run this the default post type is not listed as it doesnt have the subtype custom field. How can I only apply this filter (meta_query) to the custom post type in this query and not have it apply to the default post type 'post'?
Thanks in advanced,
Kris...
UPDATE:
OK, I have tried this but with no success - any ideas?
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'subtype',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'subtype',
'value' => 'review',
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);
This will check the 'subtype' only if it exists in the post (not tested)
$type = array( 'post', 'review' );
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
'relation' => AND,
array(
'key' => 'subtype',
'compare' => 'EXISTS'
),
array(
'key' => 'subtype',
'value' => 'review'
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);