Wordpress Posts Foreach sorting and Grouping

HI,

I have a wordpress page which im setting up to show the dates and locations of concerst my band has booked. In wordpress there are only two fields, title and post. i need to use three. to get round this i have made the title have the date and venue seperated by a delimiter in this case the vertical line |. i then use the explode function to split the two variables out for the display. so far the code I have is working fine, here it is

[php]<?php
$posts = get_posts(‘category_name=Engagement’);

if (empty($posts)) {
echo “

We have no Engagments booked…
” ;
} else {

foreach ($posts as $post) : setup_postdata( $post );

$ingress = get_the_title();
$postex = explode("|", $ingress );
$date = “$postex[0]”;
$venue = “$postex[1]”;
$desc = get_the_excerpt();
$unixdate = strtotime($date);
$eng_mth = date(‘F’, $unixdate);
$eng_date = date(‘d/m/y’, $unixdate);

echo "

$eng_mth

$eng_date
$venue
$desc
" ?> <?php endforeach; } ?>[/php]

This is displaying the engagement posts ok but I would like to alter how they are displayed. Specifically, I would like to show the next concert at the top of the page. If possible I would also like to specify a

for each month. so all of Junes concerts are in one
then July are in another and so on. you can see how it looks http://dev.hatfieldband.co.uk/bookings.php. I would like the grouping to look similar to http://www.hatfieldband.co.uk/bookings.html.

Thanks

You need to use an events plug-in. There are several free that are very good.

I agree with [member=72272]astonecipher[/member] the best way to handle this is with an Events plugin. I use the one from Time.ly on several sites. It will do all you want and more.

Thanks, I have looked at a couple of Event plug ins as you suggested already but they will not do what i want to achieve.

I want the list to integrate with the sites design. The site is not actually a wordpress site. It has been written entirely in HTML and CSS (not by me). I am just trying to integrate posts from wordpress so they fit in with the theme of the site. this iwll allow me to give non-it people within my organisation access to a basic posting page to add news items and engagements etc.

I must admit I am now totally confused.

In your OP you said

I have a wordpress page which im setting up to show the dates and locations of concerst my band has booked.

Now you have said

I want the list to integrate with the sites design. The site is not actually a wordpress site.

Can you explain a little more clearly exactly the setup, what is where and what you want to achieve?

Sorry, what I meant to say in the OP is that I have an engagements page which I want to integrate wordpress posts on.

I dont know how to generate a page within wordpress which would look identical to the site. I can however get the posts made to specific categories in wordpress from a PHP call so that I can show them on my page in the relevant DIV. the problem is the date sorting. I can sort only by the date that the posts were made. I want to sort by the date from within the most title.

Thanks

This is what I am picking up,

You have a straight HTML site. You are creating custom posts in WordPress to act as an event system. You need the events stored in the WordPress post categories to show the the straight Html site.

Correct?

Hi astonecipher,

yes that is what I want to do. the pages are .php but are essentially written in HTML. The displaying of the posts is not the problem though, that all works fine…

The issue or problem I have is creating a sort order and grouping for the posts based on a date variable which is declared within the foreach statement.

Thanks

Personally, I think you are going about this in a rather peculiar manor. I would focus on theming the site, not creating WordPress outside of WordPress.

You are going to run into a few issues with sorting. You aren’t actually giving it anyway to sort by. It can’t sort by date when the date is apart of something else.

My suggestion is still, use an event plugin. I have had amazing success using Event Espresso. It allows you to sort in many ways and hide events that are on dates that have already passed. They also have very good documentation on how to configure it many different ways.

If you still want to go the other route, this may help in that endevour, Custom Post Queries

If you use an Events plugin in Wordpress it will give you the flexibility that you need. You can then just pull an RSS feed from the Wordpress site to your static site. An Events plugin gives you the ability to have an event date that you can sort on. An alternative is a custom post type with date as a separate field that you can then sort on.

How you have created the events is what is causing you the issue. Rethink this and it may present another solution.

Another option, that is kind of plugging something that I work on, is OneCal. It gives you a feed that you can use however you want to. I am working on retheming it this week, but it gives what you are after.


http://onecal.co/

the whole idea behind making these changes is so I can give some control to a non IT person to update the news feed and engagements. My idea was to set it so they could create posts using a simple two field form or by email. I dont want to go through the whole process of redesign/rewriting into wordpress.

I appreciate that this is not the normal way to show wordpress posts. But removing the wordpress element for a second, all i want to know is how to sort an array using one of the elements from within it.

I have registered for the onecal and will see if that can help me.

Thanks

AFAIK you can’t sort on part of a field (column) from a MySQL database.

One way is to read all the posts in to an array, split them and then sort on the appropriate array element.

Here is a code snippet that allows to sort on a chosen field.

[php]
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}

 asort( $ret );

$array = $array=$ret;

return $array;

}
[/php]

Or you could do it through the sql query itself.

Thanks, I will have a play around with these later tonight.

Sponsor our Newsletter | Privacy Policy | Terms of Service