Only display upcoming events in Wordpress Widget - wordpress

Through a shortcode I placed the following code which is intented to display a random upcoming event in the sidebar. At the moment it displays a random of ALL events. How would I change the code to only display events that are younger than the current date?
<?php
// Build a custom query to get posts from future dates.
$querystr = `
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'f21_event_startdate'
AND STR_TO_DATE(wpostmeta.meta_value,'j. M Y') >= CURDATE()
AND wposts.post_status = 'publish'
AND wposts.post_type = 'events'
ORDER BY STR_TO_DATE(wpostmeta.meta_value,'j. M Y') RAND
LIMIT 1
`;
$events = $wpdb->get_results($querystr, OBJECT);
if ($events):
global $post;
foreach ($events as $post):
setup_postdata($post); ?>
<?php the_field('f21_event_startdate'); ?>
<h2><a style="color:#1e73be;" href="<?php the_permalink() ?>"><?php the_title(); ?></h2>
<div style="margin-top:10px; margin-bottom: 10px;"><?php the_post_thumbnail('full'); ?></div></a>
<b><?php the_field('f21_event_sub_header'); ?></b>
<?php endforeach;
else : ?>
<li>Sorry, no events coming up.</li>
<?php endif; ?>

you can WP_Query(). check the code below.
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'f21_event_startdate',
'meta_value' => date('Ymd'),
'meta_compare' => '>=',
'orderby' => 'rand',
'order' => 'ASC'
);
$upcoming_events = new WP_Query( $args );

Avoid the SQL query in WordPress for getting post-type data.
we have a function for that please use
$args = array(
'post_type' => 'tribe_events',
'post_status' => 'publish',
'posts_per_page' => 10,
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => '_EventStartDate',
'meta_type' => 'DATETIME',
'paged'=>$page,
'meta_query'=>array(
array(
'key' => '_EventStartDate',
'value' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
'compare' => '>'
),),
'tax_query'=>array(array(
'taxonomy' => 'tribe_events_cat',
'field' => 'id',
'terms' =>$tribe_events_cat,
))
);
$events = new WP_Query($args);
Summary: if you are using custom fields in WordPress make sure your custom fields are stored as a post meta then you can easily filter that,
In the above code, we have a meta query where we can use key, value, and compare. thanks.

Related

Wordpress display posts from multiple value in meta

I have got main product with meta key: addons and meta values in this key: 129456,968945,495435 Each of these three numbers is the key with these meta values. For example:
Post 1: meta_key: subproduct meta_value: 129456
Post 2: meta_key: subproduct meta_value: 968945
Post 3: meta_key: subproduct meta_value: 495435
And now I want to display these three posts in the main product. My code:
<?php if (!empty($addons = get_post_meta(get_the_ID(), 'addons', true))):?>
<?php
$params = array(
'post_type' => 'product',
'meta_key' => 'subproduct',
'meta_value' => $addons
);
$wc_query = new WP_Query($params);
?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<?php include(rh_locate_template('inc/parts/woomain.php')); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif;?>
With one meta value it worked but with several it doesn't work anymore. How do you view these three posts?
Try to alter your query like this and lets see if that works
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'subproduct',
'value' => array($addons),
'compare' => 'IN'
)
)
);
It works with this code:
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'subproduct',
'value' => $addons,
'compare' => 'IN'
)
)
);

Why isn't this Wordpress query sorting properly

I have a custom post_type called special_listing which contains a custom field called listing_index. A special_listing with a listing_index of 20, should appear before one with an index of 15, then 10, and so on. However this doesn't appear to be working and listings appear in whatever order they choose.
I'm not a PHP or Wordpress guy, I mainly deal with ASP.Net and C# so this is a bit confusing for me. Is there some simple mistake I'm making?
Function:
// Get the first $count listings with the highest indices for a given $region_slug:
function get_listings($region_slug, $count) {
$args = array(
'post_type' => 'special_listing',
'meta_key' => 'listing_region_slug',
'meta_value' => $region_slug
);
$posts = get_posts($args);
sort_array_on_field($posts, 'listing_index', 'DESC');
truncate_array($posts, $count);
return $posts;
}
View:
<?php
$listings = get_listings(get_microsite_slug() . '-microsite-home-featured', 4);
$i = 0;
if (sizeof($listings) > 0) : while ($i < sizeof($listings)) : $listing = $listings[$i]; // Loop and set current listing
?>
<section>
<a href="<?php echo $listing->destination; ?>">
<h3><?php echo $listing->post_title; ?></h3>
<p><?php echo $listing->post_content; ?></p>
</a>
</section>
<?php $i++; ?>
<?php endwhile; ?>
<?php endif; ?>
My attempt:
I don't know how to return the listing_index value
function get_sorted_listings($region_slug, $count){
$args = array(
'post_type' => 'kodakalaris_listing',
'meta_query' => array(
'relation' => 'AND',
array (
'key' => 'listing_region_slug',
'value' => $region_slug
),
array (
'key' => 'listing_index',
'value' => ''
),
orderby: 'listing_index',
order: 'DESC'
)
);
$posts = get_posts($args);
truncate_array($posts, $count);
return $posts;
}
Update
Trying a new way and it now pulls the content in by the published date. Still not ordering by listing_index, but at least it's not completely random either. I began looking to meta_query. Won't this only return a result with a single value? I've also tried out this other SO answer, but I find it's implementation confusing.
<?php
$args = array(
'post_type' => 'kodakalaris_listing',
'meta_key' => 'listing_region_slug',
'meta_value' => get_microsite_slug() . '-microsite-home-featured',
'posts_per_page' => 4,
'order' => 'DESC',
'orderby' => 'listing_index'
);
$listings = new WP_Query($args);
if ($listings->have_posts()) : while ($listings->have_posts()) : $listings->the_post();
?>
...
...
<?php
endwhile;
endif;
?>
You don't need to sort after you query. WP_Query has orderby parameters:
function get_listings($region_slug, $count) {
$args = array(
'post_type' => 'special_listing',
'meta_key' => 'listing_region_slug',
'meta_value' => $region_slug,
'orderby' => 'meta_value_num'
);
$posts = new WP_Query($args);
return $posts;
}
You'll then want to use The Loop rather than a foreach loop, in your view.
So I was able to figure this out after reading this article, but basically assigning a name to each meta_query array allows you to call which one should take priority when using orderby.
$args = array(
'post_type' => 'special_listing',
'posts_per_page' => 4,
'orderby' => 'index_clause',
'meta_query' => array (
'site_clause' => array (
'key' => 'listing_region_slug',
'value' => get_microsite_slug() . '-microsite-home-featured'
),
'index_clause' => array (
'key' => 'listing_index',
)
)
);
Change the function to :
<?php
// Get the first $count listings with the highest indices for a given $region_slug:
function get_listings($region_slug, $count) {
$args = array(
'post_type' => 'special_listing',
'orderby' => 'listing_index',
'order' => 'DESC',
'meta_key' => 'listing_region_slug',
'meta_value' => $region_slug
);
$posts = get_posts($args);
return $posts;
}

WP magic fields query_posts

I'm trying to display titles from posts of type 'products' with that have a custom field "product_category" (dropdown) with value "food".
When the "food" field is set to type "text" everything is fine. When I change the type of this
field to "dropdown" nothing appears.
To create custom page and custom fields I use Magic Fields plugin in Wordpress.
Here's the code:
<?php
global $wp_query;
query_posts(array(
'post_type' => 'products',
'meta_query' => array(
array(
'key' => 'product_category',
'value' => 'food',
)
))
);
while(have_posts()) : the_post(); ?>
<?php $key = get_post_meta($post->ID, 'title'); ?>
<li <?php post_class(); ?>><?php if($key) { echo $key[0]; } else { the_title(); }; ?></li>
<?php
endwhile;
wp_reset_query();
?>
use meta_key and meta_value
<?php
global $wp_query;
query_posts(array(
'post_type' => 'products',
'meta_query' => array(
array(
'meta_key' => 'product_category',
'meta_value' => 'food',
)
))
);
?>
You need to add the parameter metacompare to the array (meta_compare => 'like') because on table wp_postmeta the key_value is saved like "a:1:{i:0;s:7:"Food";}"
so
you should change to:
query_posts(array(
'post_type' => 'products',
'meta_key' => 'product_category',
'meta_value' => 'food',
'meta_compare' => 'like'
))
);
Try using meta_query instead. Make sure you try both products and product as your post_type parameter.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'product_category',
'value' => 'food'
)
)
);
$query = new WP_Query( $args );
Also, don't use query_posts. Here's why.

WordPress - How to minimize my WordPress Query?

I have a Wordpress query that I am running. It queries through days of the week, and at the moment, the only way I know to repeat the query for each day, is to run the query 7 times, manually changing the day of the week.
Is there a way to only run the query once?
My code:
<?php
$args=array(
'taxonomy' => 'day',
'term' => 'monday',
'post_type' => 'schedule',
'meta_key' => 'tr_show_time',
'orderby' => 'tr_show_time',
'order' => 'asc',
'posts_per_page' => 24,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
My code to execute for the schedule.
<?php endwhile; } wp_reset_query(); ?>
So at the moment, I have to change the "term" in each query, and have the same query 7 times. There must be a better way.
If there is a better way, can someone answer and teach me the answer?
Thanks
EDIT: I just want add that each "day" on the schedule has 24 items, one for each hour of the day, and they are displayed in order from 00:00 until 24:00 (00:00, 01:00, 02:00, etc) so this "ordering" needs to be maintained. The only way I could see to do this was to query each day seperatly, hence why I had 7 queries.
I solved it myself:
<?php
date_default_timezone_set('America/New_York');
$d=date("D");
$current_day = date ('l');
?>
<ul>
<?php
$args=array(
'taxonomy' => 'day',
'term' => $current_day,
'post_type' => 'schedule',
'meta_key' => 'tr_show_time',
'orderby' => 'tr_show_time',
'order' => 'asc',
'posts_per_page' => 24,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $show_name = get_post_meta( get_the_ID(), 'tr_show_name', true ); ?>
<?php $show_time = get_post_meta( get_the_ID(), 'tr_show_time', true ); ?>
my code here.
<?php endwhile; } wp_reset_query(); ?>
</ul>
Try using the tax_query parameter
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'day',
'field' => 'slug',
'terms' => array( 'monday', 'tuesday', 'wednesday' )
)
),
'post_type' => 'schedule',
'meta_key' => 'tr_show_time',
'orderby' => 'tr_show_time',
'order' => 'asc',
'posts_per_page' => 24,
'caller_get_posts'=> 1
);
For more information read up on taxonomy queries

Wordpress custom field date, display post with custom date > current in ascending order

How do I order post by a custom field if "releasedate" > "currentdate" but in acending order. Basically only display post with dates starting after today but in ascending order. currently i have
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=10&post_type=movies&meta_key=releasedate_value&orderby=releasedate_value&order=ASC');
if (have_posts()) : while (have_posts()) : the_post();
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
?>
<?php if ($releasedate > $currentdate) {?>
"my contents/ post here"
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
now everything works except when its ASC and not DSC, no post will display because wordpress in getting the post first and the erasing the post that are not before current date and only 10 post are allowed, therefore if 10 post has a release date before today, wordpress loads then erase them after and leaves everything blank! Thank you for your help
I'd put the date criteria into the query itself. Assuming you have WordPress version 3.1 or higher, you can use the meta_query parameter. Something like:
<?php
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$wp_query = new WP_Query( array ('showposts' => 10,
'post_type' => 'movies',
'meta_query'=> array(
array(
'key' => 'releasedate_value',
'compare' => '>',
'value' => $currentdate,
'type' => 'DATE',
)),
'meta_key' => 'releasedate_value',
'orderby' => 'meta_value',
'order' => 'ASC'
)
);
should work.
This works with me too:
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$args = array(
'post_type' => 'event',
'meta_key' => 'date_field',
'posts_per_page' => 5,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_field',
'value' => $currentdate,
'compare' => '>=',
'type' => 'DATE'
),
),
);
$my_query = null;
$my_query = new WP_Query($args);

Resources