Trying to reorder posts by event date - php

I'm using "the events calendar" plugin in WP which has a feature to allow event posts to show up as regular posts. I currently have a slider on my homepage that I would like to show featured events on. I was able to get it to show events as a "featured" category, but am having trouble ordering it by event date instead of publish date. This is what I have now.
Here is the original code just calling the specific category posts
<?php query_posts ('category_name=' .$slide.'&posts_per_page='.$bvkPP.'&paged='.$paged ); ?>
This is what I changed it to
<?php query_posts( array ('category_name=' .$slide.'&posts_per_page='.$bvkPP.'&paged='.$paged, 'orderby' => 'meta_value','meta_key' =>'_EventStartDate','order' => 'ASC',) ); ?>
This affectively ordered it by event date, but has overridden the category and is just calling for all events. Any thoughts on how to get just the specific category to show up?
Thanks!

I finally got it figured out. Here's what I came up with.
<?php query_posts( array(
'category_name' => $slide,
'posts_per_page' => $bvkPP,
'paged'=> $paged,
'orderby' => 'meta_value',
'meta_key' => '_EventStartDate',
'order' => 'ASC',
'eventDisplay'=> 'startDate',
'post_type'=> 'tribe_events' ) ); ?>
Thanks for the help!

get all event using your cat id.
<?php
$paged = ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 );
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'tribe_events_cat',
'field' => 'id',
'terms' => '17' //17 is cat id
)
),
'eventDisplay'=>'startDate',
'post_type'=>'tribe_events',
'orderby'=>'meta_value',
'meta_key'=>'_EventStartDate',
'posts_per_page' =>10,
'paged' => $paged,
'order' => 'ASC',
);
query_posts($args);
while ( have_posts() ) : the_post(); ?>
//your code here for get title/content
<?php
endwhile;
wp_reset_query();
?>

Related

Wordpress category of CPT pagination page 2 ERROR

I got problem of my pagination in category pages >>
I made a category taxonomy for my CPT and when I add posts and posts goes to page 2 .. the page 2 got a ERROR not found
<?php
// ==== Query Dynamic Options ====//
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => $paged,
'order' => 'DESC',
'orderby' => 'ID',
'taxonomies' => 'portfolio-categories',
);
$the_query = new WP_Query( $args );
if you wanna see more code that will helps u to help me ask it for me I will capture it

Wordpress custom query: 'orderby' => 'date' not working when using multiple post types

In a Wordpress page template, I set up a WP custom query which queries a custom post type named "recipe" AND the regular posts as below. This works, but 'orderby => 'date' in there doesn't work: The page first lists the regular posts ordered by date, then the recipes by date. But I need ALL of them together (i.e. mixed) ordered by date.
Here's the definition of my custom query:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...followed by the loop...]
I am grateful for any hints what I can do to achieve the desired ordering.
I found a solution myself:
It works when I add remove_all_filters('posts_orderby'); before the custom query is defined. Obviously this resets any other ordering and allows the 'orderby' => 'date' to function as expected. Complete code:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
remove_all_filters('posts_orderby'); // ADDED
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...]
Q1. what template file are you using?
The query below successfully merges the 2 post types together by date.
<?php
$args = array(
'post_type' => array('my_custom_post_type', 'post'),
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
// loop your content here
<?php include(locate_template('templates/content.php')); ?>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

Show pagination and exclude category

So, I am trying to show items from a specific category in the woocommerce:
Here is what I have so far:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => '', 'orderby' => 'date', 'order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
<div class="content"> Content </div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
When the product_cat is empty, it shows all the items. I want to include "exclude category".
For example, I want to show all but items in a "no_good" category.
Could someone help me out with it?
Also, how can I add a pagination to this?
Thanks!
Is product_cat your custom taxonomy? If it is then you need to modify your $args with tax query:
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'tax' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => array('no_good'),
'operator' => 'NOT IN',
),
),
);
...
This assumes that "no_good" is a product_cat name. Adjust field if it isn't.
Regarding the pagination part, do check the codex article regarding pagination.

How to get best selling products by period in WooCommerce

I want to show the top selling products in WooCommerce for the last 30 days. Just to show the top sellers overall is no problem with the provided code.
What the code is missing is the part where you get sales in a certain period of time. The meta_query should be the key to do this but I'm not sure how. Any help is appreciated!
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
You can use this short code for best selling product in Woo commerce.
[best_selling_products] in page edit.
OR
Also add on php file where you want like
do_shortcode('[best_selling_products per_page="12"]');
Just Change your code like below
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'meta_key' => 'total_sales',
'orderby' => array( 'meta_value_num' => 'DESC', 'title' => 'ASC' )
);
Hope its work.

Wordpress - Fetching all data and sorting

I am using Wordpress. I have the following query to fetch data from database and it is working perfectly
$args1 = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'post__in' => array(400, 403),
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC'
);
query_posts($args1);
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//And then some other code to display data
Using 'post__in' => array(400, 403),in the above query I am fetching the rows where ID='400' AND '403' . So when I echo, I get to see only two information.
Now, what I am trying to achieve is to fetch all data from the table but when I display the information I want to get the row where ID is 400 at first then 403 and then rest of the rows based on 'orderby' => 'title', AND 'order' => 'ASC'
Could you please help with the query?
Thanks
Edit
$args2 = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'post__not_in' => array(400, 403),
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC'
);
query_posts($args2);
Not sure if this will work within the query_post argument, but it is supplementing valid SQL in to it. Try:
$args = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'paged' => $paged,
'orderby' => 'case when ID in (400,403) then -1 else title end, title',
'order' => 'ASC'
);
query_posts($args);
That is not going to be possible using the Wordpress query structure like you are doing. One possible solution is to do a second query looking for results not in (400, 403) and simply add this array of results to the end of your first array.
In order to do this, you should probably use get_posts() instead of query_posts() so that it doesn't alter the main loop.
$array = get_posts($args1);
$array = array_merge($array, get_posts($args2);
I suppose you could try doing the following:
$args = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'paged' => $paged,
'orderby' => 'ID = 400 DESC, ID = 403 DESC, title',
'order' => 'ASC'
);
query_posts($args);
...
Let me know how this goes.

Categories