Trying to pull post type according to their excerpt value.
my code so far:
$ids = array(111,333,9061);
$args = array (
'post_type' => 'kb',
'posts_per_page' => -1,
'meta_query' => array (
array(
'key' => 'excerpt',
'value' => $ids,
'compare' => 'IN'
)
)
);
$the_query = new WP_Query( $args );
If I remove the meta_query it pulls the posts.
Also tried with 'key' => 'post_excerpt'
Any ideas?
Related
I would like to retrieve all those posts whose author (post table field) is a given one OR those which has a given meta value (postmeda table field).
If "author" was a meta value, I know I could use a meta_query to achieve it. The thing here is that it is not... so I think I cannot use the "author" field within a meta_query and use the "relation" key.
I'm looking for something like:
$args = array(
'post_type' => array('post'),
'orderby' => 'ASC',
'order' => 'date',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'field' => 'author',
'value' => $author_id,
'compare' => '==',
),
array(
'key' => '_meta_field_name',
'compare' => 'NOT EXISTS',
),
),
array(
'relation' => 'AND',
array(
'key' => '_meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => '_meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
),
);
$data = new WP_Query( $args );
Any suggestion on how to achieve that using WP_Query?
Thanks!
You might like to try an approach like this one instead. The idea is to query the two conditions you want to search for, then merge the two queries into one finished product.
//Get posts with the author you're looking for
$args1 = array(
'author_name' => 'testuser', //or 'author' => $author_id or something else
);
$data1 = get_posts( $args1 );
//Get posts with the meta data you're looking for
$args2 = array(
'meta_query' => array(
array(
'key' => 'meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => 'meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
);
$data2 = get_posts( $args2 );
//Merge both arrays
$allData = array_merge( $data1, $data2 );
//Get just the IDs of all the posts found, while also dropping any duplicates
$postIDs = array_unique( wp_list_pluck( $allData, 'ID' ) );
//Do a new query with these IDs to get a properly sorted array of post objects
$args3 = array(
'post__in' => $postIDs,
'order' => 'ASC',
'orderby' => 'date',
);
$finalAnswer = get_posts( $args3 ); //This is your array of post objects. Ta-Da!
I am trying to run custom loop for Custom Post Type with filtering by some meta_key and value
Now simple meta value works fine but here I have a challenge with the below kind of serialized data (nested).
I am using wpalchemy for meta box. meta_key for the post type is _event_meta and value is as below
a:9:{s:19:"ac_event_operations";a:1:{i:0;s:8:"Training";}s:18:"ac_event_positions";a:1:{i:0;s:10:"Supervisor";}s:18:"ac_event_employees";a:1:{i:0;s:2:"15";}s:13:"ac_event_date";s:10:"2017-06-15";s:19:"ac_event_start_time";s:5:"06:30";s:17:"ac_event_end_time";s:5:"07:00";s:14:"ac_event_place";s:6:"Office";s:18:"ac_event_organizer";s:4:"Jack";s:16:"ac_event_contact";s:4:"Rose";}
I am trying to filter All Events based on ac_event_operations, ac_event_positions, ac_event_employees
So for me, the challenge is to get events filtering with the value from above meta_keys. Here is the query I used but of course, it is not giving any result.
global $event_mb;
$meta = get_post_meta( get_the_ID(), $event_mb->get_the_id(), TRUE );
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = [
'post_type' => 'event',
'posts_per_page' => get_option( 'posts_per_page' ),
'paged' => $paged,
'meta_query' => [
'meta_key' => '_event_meta',
'value' => '%ac_event_employees%',
'compare' => 'LIKE'
],
];
$temp = $wp_query;
$wp_query = NULL;
$wp_query = new WP_Query( $args );
Use like below,
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'country',
'value' => 'Israel',
'compare' => '='
),
array(
'key' => 'age',
'value' => array( 20, 30 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$user_query = new WP_User_Query( $args );
For more Ref : https://codex.wordpress.org/Class_Reference/WP_User_Query
I'm using this query do get some posts.
$args = array(
'post_type' => 'spaces',
'post_per_page' => '500',
'orderby' => 'rand',
'meta_key' => 'space-city',
'meta_value' => $search,
);
$query = new WP_query($args);
Now I need to order the results by total of comments on each post. I've a custom field with this number called "space-comments", but I've no idea how to sort this posts with this second meta_key.
I made some tests, but I only was able to get post when "space-comments" has a value. When there is no value, the post don't show up.
Any ideia how can I start?
The WP_Query can accept a meta_query argument that is populated as an array of sub-arguments. That array can have sub arrays that are each their own meta query, so you can create some nice compound searches across meta data.
See the example from https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters below.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
I'm trying to sort out a WordPress query for a Custom Post Type, but I can't make it work.
The post type is events. An Advanced Custom Fields field called sticky (yes/no) is used for sorting (stickies on top), and another ACF field called glamrock (yes/no) is used to exclude certain posts.
The result is, glamrock gets excluded, but stickies are not sorted.
If I delete the meta_query, sorting works fine, but glamrock posts are included.
$bpb_args = array(
'numberposts' => -1,
'post_type' => 'events',
'posts_per_page' => 100,
'paged' => get_query_var( 'paged', true ),
'meta-key' => 'sticky',
'meta_query' => array(
array(
'key' => 'glamrock',
'value' => 'no',
'compare' => 'IN',
)
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
$bpb_query = new WP_Query( $bpb_args );
if( $bpb_query->have_posts() ):
while( $bpb_query->have_posts() ) : $bpb_query->the_post();
//show post
endwhile;
endif;
Update:
Unfortunately, meta_value instead of meta_value_num didn't change anything. It still seems to be sorting by date/time.
The Advanced Custom Field type is Radio Buttons.
In addition to the arguments, I also included the loop.
You need to specify only meta_value since your meta-key is non numeric
'orderby' => 'meta_value'
#Mihai had it right: meta_value_num was the main issue. I changed it to meta_value.
Here's the query/loop that worked:
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'sticky',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'lizenz_erteilt',
'value' => 'no',
'compare' => 'LIKE',
),
)
);
$query = new WP_Query( $args );
// Loop
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
//show post
endwhile;
endif;
My issue is pretty simple, I'm trying to get both portfolio posts (from a plugin) and articles that have the "portfolio" category (made myself).
$args = array(
'post_type' => 'portfolio',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'portfolio',
)
)
);
$query = new WP_Query($args);
It looks like the relationship between post_type and tax_query is an AND, I'd need an OR, how can I do that?
I tried following the official documentation but I'm stuck. (https://codex.wordpress.org/Class_Reference/WP_Query)
Thanks for the help.
Here is an example with a relation 'OR':
$args = array(
'post_type' => 'portfolio',
'meta_query' => array(
'relation' => 'OR', /* <-- here */
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
I haven't found any solution using only one query. (couldn't change the AND to OR).
So I've made two queries instead:
$query1 = new WP_Query(array('post_type' => 'portfolio'));
$query2 = new WP_Query(array(
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'portfolio',
)
)));
$query = merge_WP_queries($query1, $query2);
function merge_WP_queries(WP_Query $query1, WP_Query $query2){
$wp_query_returned = new WP_Query();
$wp_query_returned->posts = array_merge( $query1->posts, $query2->posts );
//populate post_count count for the loop to work correctly
$wp_query_returned->post_count = $query1->post_count + $query2->post_count;
return $wp_query_returned;
}
It works correctly, even though it's not what I'd like to use.