In Wordpress: Group same-day posts on the index page - wordpress

i'm looking for a way to code the loop in such a way that it places posts that are in the same day within a div. Im doing this so I can put a separator between the different days to show users that they are looking at another day. An example of this in action is in informedlondon.com, where once a days worth of posts ends, there is a little graphic in between to separate the posts. This is what Google's Blogger platform does by default, thats why it was easy for the divider to be put between the different days of posts.
I'm guessing there must be a tutorial about it somewhere but I have checked and can't seem to find one. I have a sneaky suspicion that it's very easy and i'm just being a dunce. Would appreciate some help.
Many thanks in advance.

The simplest method to accomplish things like this is to 'hold' the current date in a variable. Before your loop: var $current_date and then inside your loop, a simple if statement:
if( $current_date != get_the_date('d-m-Y') ){
echo '<h2>Posts for '.get_the_date('d-m-Y').'</h2>';
$current_date = get_the_date('d-m-Y');
}
So in effect, you're checking with each post whether the date of the post is different than the date of the last post rendered (in the 'holding' variable, $current_date). If it is, you print a title with the new date, and set the 'holding' variable to the new date. If not, nothing happens and the post is printed as usual.

Related

How to check if an order number exists in woocommerce?

In my WooCommerce store, I have an input field where I want the user to enter the order number. When validating this field, I need to check if that order number actually exists in WooCommerce.
From this question, it seems like the WooCommerce order_number might not always be the same as the order_id (which is always the same as the post_id).
So I can't use wc_get_order($the_order) function since the documentation states that it wants the post_id in the $the_order parameter.
I can't find a specific function to get the order by the order number (neither in the documentation nor the code in github).
I'm fairly new to WooCommerce, so maybe I am missing something. Is there a way to do this at all? Or is the wc_get_order mis-documented?
I've done a lot of googling and searching here on stack overflow, but I really can't find the answer to this! Any help is appreciated.
PS: I suppose I could get all orders and loop through them one by one checking if the number matches, but I was hoping to find a simpler solution to this :D
You may try this if statement to see if given ID (number) is an Order Number or not:
function is_it_a_shop_order($givenNumber)
{
if(get_post_type($givenNumber) == "shop_order")
{
echo "yes this is a valid order number";
}
else
{
echo "no go away";
}
}
Wordpress stores all posts (pages, orders, products etc.) in wp_posts table and orders are stored with post type named "shop_order".
I tested the code. I hope this will help you. Have a good day.
The reason you're struggling to find anything about this in the documentation is because WooCommerce has no native concept of an order number. The ID is the order number.
What store owners tend to find however is that order IDs don't increase sequentially. There can be a large jump in ID between one order and the next because of the way WordPress handles posts. Often users will install a plugin to address that. It's those plugins that introduce the differentiation between order IDs and what they refer to as the order number.
Here's an example of one such plugin: https://en-gb.wordpress.org/plugins/woocommerce-sequential-order-numbers/
Included in the documentation is an example of how to find an order by its number. If you have one of these plugins installed, you'll simply need to lookup how the plugin in question resolves numbers to IDs.
Send the order number through the wc_get_order call. If the returned array is empty, the order number does not exist.
$bOrderExists = DoesOrderExist($OrderNumber);
function DoesOrderExist($OrderNumber)
{
$orderq = wc_get_order($OrderNumber);
if(empty($orderq))
return 0;
return 1;
}

Wordpress display the posted date

I display a list of my recently posted custom posts. However i would like to display when it was posted with the get_the_date() function.
I would like the date to be displayed like this.
If it is posted today: just the time it was posted (18:24)
If it was posted this year: The day + month (22 may)
If it was posted before the current year: The normal date (2016-10-11)
Is this possible to do?
Thanks in advance! /Molle
Yes, this is possible to do, provided you can write PHP yourself within your WordPress page or create your own template to take advantage of said PHP.
If you have the date of the post loaded in a variable already (assuming you do), then you can use your own logic (if, else is statements as literally stated by your question) to parse out the day and year.
Read more on date formatting to get the desired output you wish:
http://php.net/manual/en/datetime.formats.date.php

Can I get a specific number of posts per author and order by date in Wordpress?

I have a pretty basic query_posts call:
query_posts(array(cat=>3,author=>$userID,posts_per_page=>12))
This is getting 12 posts from the third category for a set of authors. The only piece I'm missing is I want those 12 to be made up of two posts each of my 6 authors. The order by date is fine, they don't need to be grouped by author.
Is there a way to pull posts this way?
I could be wrong, but I don't think this is possible with just one query since there isn't (to my knowledge) a way to specify how many posts per author.
It will add to your load time a little bit, but you might have to split it out into 6 different queries to get it to work.
You can do this by using WP_Query (your probably shouldn't use query_posts as it will change your main query) and wrapping each query in the specific author block:
$author1 = new WP_Query(array('cat'=>3,'author'=>$author1ID,'posts_per_page'=>12));
if($author1->have_posts):
while($author1->have_posts()):
$author1->the_post();
?>
<!--Insert HTML etc. here-->
<?php
endwhile;
endif;
wp_reset_query(); //reset the query object if running in the loop
Then do it for each of your other authors and that should do the trick.
Hope that helps. If there is a way to do one query I would definitely like to know since multiple queries can slow down page load.

Insert a Variable within a WP_Query (Wordpress)

I am trying to get wordpress to pull an ID from a variable to use within a category query but for some reason it isn't working. It's probably something with the syntax could you just give me a helping hand.
Here is what I have...
$catPosts1 = new WP_Query('category=$cat1&offset=5&posts_per_page=3');
Basically what I want it to do is get the category ID from $cat1 (I have tested and it is entering a category id in the variable), offset the number of posts by 5 and display 3 posts linked with that category. At the moment the code is just displaying posts offset by 5.
Any ideas?
Mark
Now you're just sending $cat1 along as a string, the code should look like this.
$catPosts1 = new WP_Query('category='.$cat1.'&offset=5&posts_per_page=3');
If it is showing, literally, "$cat1" in the output, you might need to switch from single quotes to double quotes to get the substitution.
That is to say, do this:
$catPosts1 = new WP_Query("category=$cat1&offset=5&posts_per_page=3");
...if you're trying to get the contents of the variable into the WP_Query call.
use double quotes instead of single.it is simple.

Wordpress - Sorting posts by expiry date

I'm trying to show expired posts AFTER non-expired posts. How can I do this?
I've added a custom field "Expiration Date", in which I store the expiration dates in yyyy/mm/dd format. Problem is, if I order my results by this field, future expiry dates come first.
So I created a repeating cron-job which compares the dates and creates a secondary custom field "Expiration Date Passed" for posts whose dates have passed. I tried ordering by this field, but WP only shows posts with a value for this field - IE posts with no expiry date, or expiry dates in the future, don't show. So I tried auto-adding values '99999999' for any post which haven't expired yet. Problem is, WP can't order by custom field values THEN date- IE the first posts with value '99999999' are in a random order.
I also tried doing two queries for posts, one without expired posts, one with, then merging these two arrays. So the data is in the right order - but it screwed up WP's pagination.
Help, I'm running out of ideas!
Since you have an "Expiration Date Passed" custom field , you could first create two sets of Posts using that custom field in your get_posts arguments to differentiate between current & expired Posts
$meta_key and $meta_value
(string) (optional) Only show posts that contain a meta (custom) field with this key and value. Both parameters must be defined, or neither will work.
Default: None
extract from:
http://codex.wordpress.org/Function_Reference/wp_get_recent_posts
then you'll be able to sort each set the way you want
That might work, but I am trying to sort the posts on my category pages. Wp_get_recent_posts function is usually used for creating custom loops, not modifying 'the loop' in category (archive template) pages.
In the end I sorted it with this. I added this code to the top of my archive template:
global $query_string;
query_posts($query_string . "&orderby=meta_value&meta_key=Expiration Date Passed&order=DESC");
I created a "sort" custom field called "Expiration Date Passed". A cron job then looks to see whether the post has an expiration date. If it doesn't, or if the date is in the future, it puts the post's publish date + 20 years in the sort column. If the post's expiration has passed, it puts the post's publish date in the sort column. Thus it results in the order I was after:
1) Posts which haven't expired, in date order
2) Posts which have expired, in date order
Thought I would post that solution in case anyone else wanted to know.

Resources