Showing upcoming events (including todays event)? - php

I have the following loop that doesn't quite work. I want to only show upcoming events including events that are taking place today.
At the moment it shows all the upcoming posts but also the posts before todays date.
Where am I going wrong?
<?php
$today = date('Ymd');
$portfolioloop = new WP_Query(
array(
'post__not_in' => array(4269),
'paged' => get_query_var('paged'),
'meta_key' => the_date(),
'post_status' => 'future,publish',
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 20,
'order' => 'ASC',
'meta_query' => array(
array('key' => the_date(),
'value' => $today,
'compare' => '>=')
),
)); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
// content here.
<?php endwhile; // end of the loop. ?>
I'll give this a go:
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
$today = date('Ymd');
$where .= " AND post_date >= '$today' ";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
<?php
$query = new WP_Query(
array(
'post__not_in' => array(4269),
'paged' => get_query_var('paged'),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 20,
'order' => 'ASC'
)); ?>

I think this might work:
<?php
function filter_where( $where = '' ) {
$where .= " AND post_date >= '" . date("Y-m-d") . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query(
array(
'post__not_in' => array(4269),
'paged' => get_query_var('paged'),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 20,
'order' => 'ASC'
)
);
remove_filter( 'posts_where', 'filter_where' );
?>

Related

How can I use sub fields in wordpress meta_query for wp query args?

Hi all I'm trying to ad a meta_query to my wp query args.
Here is my post meta:
$post_options = get_post_meta( get_the_ID(), 'post_options')[0];
print_r($post_options);
which produces an array like this:
Array ( [on_demand] => true [featured_video] => https://example.com/1234 )
Here is my code, the normal loop works fine but breaks when I add the meta_query:
//helper function from https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'post_options_%", "meta_key LIKE 'post_options_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'post_options_%_on_demand',
'value' => 'true',
'compare' => '='
)
)
);
// The Query
$the_query = new WP_Query( $args );

WooCommerce:Custom product template - Pagination isn't working

all products are displayed. "posts_per_page" is not working. I try to limit products to 12 by page, but it shows all products.
Looks like my code is fine, but it isn't working.
Whats wrong with my code?
Can someone enlighten me, please?
Here's my code:
<?php
$meta_query = array();
$meta_query[] = array('key' => '_visibility','value' => array('visible', 'catalog'),'compare' => 'IN');
$meta_query[] = array('key' => '_stock_status','value' => 'instock','compare' => '=');
if($min_price !='' && $max_price !=''){
$meta_query[] = array('key' => '_price','value' => array($min_price, $max_price),'compare' => 'BETWEEN','type' => 'NUMERIC');
}
if($orderbym != '')
{
$mkey = '_price';
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'ignore_sticky_posts' => 1,
'orderby' => $orderby,
'order' => $order,
'posts_per_page' => -1,
'meta_query' => $meta_query,
'meta_key' => $mkey,
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'bundle',
),
$product_catar
),
);
global $woocommerce_loop;
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args));
$columns = '2';
$woocommerce_loop['columns'] = $columns;
ob_start();
if($products->have_posts()){
woocommerce_product_loop_start();
while ( $products->have_posts() ) {
$products->the_post();
wc_get_template_part( 'content', 'product' );
}
woocommerce_product_loop_end();
}else{
_e( 'No product matching your criteria.' );
}
woocommerce_reset_loop();
wp_reset_postdata();
echo '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
?>
What comes to my mind is that the filter is maybe overwriting the query args. See what you get by doing
var_dump(apply_filters( 'woocommerce_shortcode_products_query', $query_args)) ;
My guess is you'll get different query args

Meta_query in WooCommerce

I'm trying to use the meta_query in WooCommerce product page.
This is the code I'm using:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' =>8,
'meta_query' => array(
array(
'key' => 'autor',
'value' => '"'.get_the_ID().'"',
'compare' => 'LIKE',
)
),
);
$products = new WP_Query($args);
if ($products->have_posts()) :
$i=0;
while ($products->have_posts()) : $products->the_post();
$autor = get_field('autor');
if($i % 2 ==0) ?>
<h3><?php the_title();?></h3>
<?php if ($i % 2 != 0)
$i++;
endwhile;endif;?>
It doesn't show any title, if I remove the meta_query it shows all products so the problem is that the relation meta_query code is not working. Any ideas how to use it on WooCommerce template?
You use get_the_ID() to get the author id in meta_query args.
get_the_ID() - will get the Post id, not the author id.
To get all posts by authoк id your args should look like this:
$args = array(
'author' => 1,
'post_type' => 'product',
'posts_per_page' => 8,
);
I also see that you using get_field()-function. WordPress core does not have this function. You can use instead get_the_author().
Eventually your code will look like:
<?php
$args = array(
'author' => 1,
'post_type' => 'product',
'posts_per_page' => 8,
);
$products = new WP_Query($args);
if ($products->have_posts()) :
$i=0;
while ($products->have_posts()) : $products->the_post();
$autor = get_the_author();
if($i % 2 ==0) ?>
<h3><?php the_title();?></h3>
<?php if ($i % 2 != 0)
$i++;
endwhile;endif;?>
You can use this code snippet for meta_query in woocommerce
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page'=> 10,
'orderby' => 'total_sales',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_featured',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'total_sales',
'value' => '10',
'compare' => '>='
)
)
);
$query = new WP_Query( $args );

Wordpress - managing event listings?

I have an events page that uses the following query:
<?php $portfolioloop = new WP_Query( array( 'post__not_in' => array(4269), 'paged' => get_query_var('paged'), 'post_status' => 'future', 'post_type' => 'whatson', 'exclude' => '4269', 'posts_per_page' => 20, 'order' => 'ASC')); ?>
All this does is show a list of all scheduled custom posts and when the post hits the scheduled date it publishes the page... thus removing it from the list.
It's nearly what I want, when it hits the publish date, the event is actually running on that day so removing it from the list isn't quite correct.
Is there a way I can delay removing it from the list until the end of the day?
p.s I don't want to use a plugin as I don't think it warrants it.
I've found this:
$args = array(
'posts_per_page' => 3,
'meta_key' => 'event-start-date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array( 'key' => 'event-end-date', 'compare' => '>=', 'value' => date('Y-m-d') )
)
);
query_posts($args);
I don't want to sort by a custom field so how can I do it by the post publish date?
Can't you just add a post_date WHERE statement to the query to search for posts? Then the post_status will have to be removed from the query, thus:
<?php $query_string = array( 'post__not_in' => array(4269), 'paged' => get_query_var('paged'), 'post_type' => 'whatson', 'exclude' => '4269', 'posts_per_page' => 20, 'order' => 'ASC'); ?>
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
$end_of_day = date('Y-m-d') . ' 23:59:59';
$where .= " AND post_date < '$end_of_day' ";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
Finally solved this with:
function my_filter_where( $where = '' ) {
global $wp_query;
if (is_array($wp_query->query_vars['post_status'])) {
if (in_array('future',$wp_query->query_vars['post_status'])) {
// posts today into the future
$where .= " AND post_date > '" . date('Y-m-d', strtotime('now')) . "'";
}
}
return $where;
}
add_filter( 'posts_where', 'my_filter_where' );
And:
<?php
$wp_query = array(
'post__not_in' => array(4269),
'paged' => get_query_var('paged'),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 20,
'order' => 'ASC',
'orderby' => 'date',
'post_status' =>array('future','published'));
query_posts($wp_query);
?>
<?php
if ($wp_query->have_posts()) {
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
Content
<?php endwhile; // end of the loop.
} ?>
<?php if (function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $wp_query ) ); } ?>

implement add_filter with LIKE %some_title% in WP_Query

I'm trying to add a filter to my WP_Query(below code) to search for post_title LIKE %some_title%
$args = array(
'post_type' => 'product',
'posts_per_page' => $page_size,
'paged' => $page,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$result = new WP_Query($args);
How to add a filter to match product title with $some_title?
you can try in this way
$query = "
SELECT *
FROM $wpdb->posts
WHERE $wpdb->posts.post_title LIKE '$param2%'
ORDER BY $wpdb->posts.post_title
";
$wpdb->get_results($query);
alternate way and standard way
add_filter( 'posts_where', 'wpse18703_posts_where', 10, 2 );
function wpse18703_posts_where( $where, &$wp_query )
{
global $wpdb;
if ( $wpse18703_title = $wp_query->get( 'wpse18703_title' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( like_escape( $wpse18703_title ) ) . '%\'';
}
return $where;
}
use in this way
$args = array(
'post_type' => 'product',
'wpse18703_title' => 'your string',
'posts_per_page' => $page_size,
'paged' => $page,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$result = new WP_Query($args);

Categories