Issue on simple WP_Query with posts meta data - wordpress

I have a custom post type 'event' and a meta field 'event_repeat'. I want to show only those events that have the 'weekly' value in 'event_repeat'.
My code for the query:
<?php
$args = array(
'post_type' => 'event',
'meta_query' => array (
'key' => 'event_repeat',
'value' => 'weekly',
'compare' => 'IN'
)
);
$events_future_query = new WP_Query($args);
?>
But the loop shows ALL events regardless the settings in 'meta_query'. I will be very thankful for you help.

Related

WordPress include custom field in search

I have a custom post type of post_type_1 in WordPress, I also have a custom field in that post type of custom_field_data
I am doing a search for apples by querying posts using wp_query like this...
$search_term = 'apples';
$args = array(
'post_type' => array('post_type_1'),
'post_status' => array('publish'),
'posts_per_page' => -1,
's' => sanitize_text_field( $search_term)
);
$results= new WP_Query( $args );
This works correctly and returns all posts with apples in the title, but I would also like to extend the search to the custom field custom_field_data so the query will return all posts with apples in either title or custom field.
What is my best approach? I have tried using a meta_query but haven't been successful. Does anybody have an example?
Use below code will work for custom field search.
$custom_field = $_GET['custom_field '] != '' ? $_GET['custom_field '] : '';
$search_term = 'apples';
$args = array(
'post_type' => array('post_type_1'),
'post_status' => array('publish'),
'posts_per_page' => -1,
's' => sanitize_text_field( $search_term),
'meta_query' => array(
array(
'key' => 'custom_field_key',
'value' => $custom_field ,
'compare' => 'LIKE',
),
)
);
$results= new WP_Query( $args );
Tested and works well.

Filter with Advanced Custom Field in WordPress

I am trying to set filters based on Advanced Custom Fields in my WordPress site. Basically my advanced custom field named as 'ispremium' has two values as 'yes' and 'no' and in dropdown filter I have set two options as 'Premium Only' And 'All Programs'.
I need to do when 'Premium Only' dropdown option is selected it will list all the post having value 'ispremium'='yes' and when all program is selected it will list both 'ispremium=yes' and 'ispremium='no'. I have following code but it is listing post with 'ispremium=yes' always. What's wrong in my code?
<select name="order" onchange="this.form.submit();">
<?php
$order_options = array(
'yes' => 'Premium Only',
'no' => 'All Programs',
);
$result = query_posts( array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'ispremium',
'value' => 'yes',
),
) ) );
$result = query_posts( array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'ispremium',
'value' => 'yes','no'
),
) ) );
foreach( $order_options as $result => $label ) {
echo "<option ".selected( $_GET['value'], $value )." value='$value'>$label</option>";
}
?>
</select>
First, try to use WP_Query instead of query_posts(). You also have some syntax errors like 'value' => 'yes','no'.
For premium only:
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'ispremium',
'value' => 'yes',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
All Programs
$args = array(
'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ispremium',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'ispremium',
'value' => 'no',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
Check ACF documentation for further info. Also, I suggest you to do this with Ajax, since if you put this code within your select element, the available options are going to be the from the last query made, and won't be dynamic on field change.
But I don't totally get what you're trying to do though; you'll probably want to show the available programs in another drop down, or you could simply redirect to another archive page which lists the programs accordingly to the selected option.

Woocommerce product filter meta key

I have built a search a form at custom page in wordpress and want to filter product on shop page using meta keys that are already exists into posts table.
Initially I have tried to filter categories at form page like this but it doesn't work.
$meta_query = array(
'key' => '_years',
'value' => '2009'
);
$args=array(
'meta_query' => $meta_query,
'posts_per_page' => 10,
'post_type' => 'product',
'orderby' => $orderby,
'order' => $order,
'paged' => $paged
);
wc_product_dropdown_categories($args);
The meta_query parameter must be change in an array of array for a single custom field handling:
$meta_query = array(
array(
'key' => '_year',
'value' => '2009',
'compare' => '>',
)
);
Some details from WP_Query page in the Single Custom field handling part
Hope it will work with this.

wp_query with post_id not working

I want to retrieve the post using the post id.
But i found that its retrieving the post based on custom post type and not by post id. This is my code. Please give me some suggestions.
$args = array(
'post_type' => 'landingpage',
'post_id' => '4110',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'page_link',
'value' => 'http://5.heypayless/Landing%20Pages/dedicated-web-programmers/',
'compare' => '='
)
)
);
$obituary_query = new WP_Query($args);
while ($obituary_query->have_posts()) : $obituary_query->the_post();
$test= get_field('price/hr_for_test_services');
$junior=get_field('price/hr_for_junior');
$senior=get_field('price/hr_for_senior');
$lead=get_field('price/hr_for_lead_designer');
$features=get_field('features_for_test_services');
$features2=get_field('features_for_junior');
$features3=get_field('features_for_senior');
$features4=get_field('features_for_lead_designer');
endwhile;
echo $post->ID;
wp_reset_postdata();
?>
$args = array( 'p' => 42, // id of a page, post, or custom type 'post_type' => 'any');
$my_posts = new WP_Query($args);
WP_Query doesn't take a post_id argument. You need to use page_id instead. The type is an int not a string:
...
'page_id' => 4110,
...

Exclude a featured post from the Wordpress loop

My Wordpress front page has both a featured article (styled inside a box) and a list of recent posts (styled separately). I want to display these recent posts using Wordpress loop excluding the featured post. It is easy to achieve excluding a certain category or a tag, but in my case I want to exclude a post with a custom field. The featured post has a custom field with a name and a value of: featured = yes.
How do I achieve this without using a plugin?
You can use meta_query parameter, as explained in http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Something like:
$args = array(
'post_type' => 'any',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'NOT LIKE'
)
)
);
$query = new WP_Query( $args );
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featured',
'value' => 'yes',
'compare' => '='
),
));
$ids = array();
$query = new WP_Query($args); // fetching posts having featured = yes
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$ids[] = $post->ID; // building array of post ids
}
}
$args = array( 'post__not_in' =>$ids); // excluding featured posts from loop
query_posts($args);
while (have_posts()) : the_post();
// rest of the code

Resources