Wordpress advanced custom field query giving wrong results - wordpress

I have the following code. It is supposed to return only the post from 'portfolio' custom post type where the format is 'print', the checkbox for 'welcome_gallery' is checked and the 'market' value is 'italy'. But it returns also other post not matching these key values.
$recent_posts = array(
'post_type' => 'portfolio',
'numberposts' => 8,
'orderby' => 'rand',
'meta_query' => array(
'relation' => 'AND',
array(
'meta_key' => 'format',
'meta_value' => '%print%',
'compare' => 'LIKE'
),
array(
'meta_key' => 'welcome_gallery',
'meta_value' => '1',
'compare' => '='
),
array(
'meta_key' => 'market',
'meta_value' => '%italy%',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $recent_posts );
I've tried hard to make it work but it doesn't. Before I get crazy, is there someone who can help me to undestand the reason?
Thanks in advance.

Related

How to query_post meta_value first then recent post in WordPress

I have a custom post meta for make pin post
I Need to query pin post first then recent post like image below
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'yp_featured_post',
'value' => 'on'
),
array(
'key' => 'yp_featured_post',
'compare' => 'NOT EXISTS'
)
);
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_query' => $meta_query,
'orderby' => array(
'meta_value' => 'ASC',
'date' => 'DESC'
),
);

order posts by meta query, if featured show first then verified then normal

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!

ACF - don't show passed events and filter with start date

I have a list of events and I'd like to do 2 things :
order posts according to starting date
don't include passed events in the query
I did some research and builded a query but it doesn't work.
'''
$current_date = date_i18n('d.m.y');
$the_query = new WP_Query( array(
'post_type' => 'spectacles',
'meta_query' => array(
array(
'key' => 'header_spec_period_start',
'type' => 'DATE'
),
array(
'key' => 'header_spec_period_end',
'value' => '$current_date',
'compare' => '>',
'type' => 'DATE'
),
),
'order' => 'ASC' ) );
?>
'''
All the events are showing, I can't get the passed events filtered.
$date_now = date('Ymd');
$args = array(
'post_type' => 'spectacles',
'post_status' => 'publish',
'meta_key' => 'header_spec_period_start',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'header_spec_period_start',
'compare' => '>',
'value' => $date_now,
),
),
);
$the_query = new WP_Query( $args );
You can try this ...

How to use custom fields in wp_query for ordering and filtering the result at the same time?

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 );

wp_query on 2 post types, one having custom field for further filtering

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',
);

Resources