Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering Wordpress posts based on parent category

UPDATE: I have tried using the following code:

<?php if (is_category(events)) { 
$posts = query_posts($query_string . '&orderby=event_date&order=desc'); 
} else {
    $posts = query_posts($query_string . '&orderby=title&order=asc'); 
    }
?>

Is there any reason why that wouldnt work? It seems to work fine organising posts in alphabetical order, but still no luck on the date order within 'events'.

--

After searching through various existing questions I can't quite find a solution to what I am trying to do.

Currently all posts on my site are ordered alphabetically, which is fine except for one new category that I have added. For this category I want to order all posts by a value that I enter into a custom field. The field is called 'event_date' - so I want to order the posts by date essentially, but not the date the post was created, the date the user manually enters into this field.

I managed to get it working by using:

<?php if (is_category($events)) { $posts = query_posts($query_string . '&orderby=$event_date&order=asc'); } ?>

However this overrides the aphabetical order for all other pages.

For alphabetical order I am using:

<?php if (is_category()) { $posts = query_posts( $query_string . '&orderby=title&order=asc' ); } ?>

Essentially I want a statement that tells the page to order all posts in aphabetical order, unless the category is 'events', where I want to order them by the custom event date.

As you can probably tell I'm very much front end, not back end so a lot of this is fairly new to me, so any help or advice is appreciated.

like image 999
Dwight Schrute Avatar asked Nov 12 '22 22:11

Dwight Schrute


1 Answers

To order posts by a custom field value, you need add the custom meta field to the query itself. orderby=meta_value in addition to meta_key=metafieldid will allow ordering in this fashion.

I would use the pre_get_posts hook and modify the query object if get_query_var( "cat" ) (or a similar query var) returns the desired post category.

add_action( "pre_get_posts", "custom_event_post_order" );

function custom_event_post_order( $query )
{
    $queried_category = $query -> get_query_var( "cat" );

    /*
     * If the query in question is the template's main query and
     * the category ID matches. You can remove the "is_main_query()"
     * check if you need to have every single query overridden on
     * a page (e.g. secondary queries, internal queries, etc.).
     */
    if ( $query -> is_main_query() && $queried_category == 123 )
    {
        $query -> set( "meta_key", "event_date" ); // Your custom field ID.
        $query -> set( "orderby", "meta_value" ); // Or "meta_value_num".
        $query -> set( "order", "ASC" ); // Or "DESC".
    }
} 

Remember that this approach overrides all queries that are using the category in question. You can build custom WP_Query objects that use their own parameters for constructing loops.

You also should standardize the way you save the custom field data to the database. For dates I prefer using UNIX-timestamp formatted times that are easy to move around and manipulate. This way no accidents happen when querying and some data is formatted in another way that the rest is.

Disclaimer: I did not have the time to test the above code in action, but the general idea should work properly.

EDIT: of course the above code should be inserted to functions.php or a similar generic functions file.

like image 88
ojrask Avatar answered Nov 15 '22 13:11

ojrask