1
votes

I'm currently working on a little website in Wordpress. I'm modifying the standard wordpress and themes/ plugins.

I make a website with an activity feed for upcoming events. (Here i use the standard Blog post in Wordpress for.)

On another page i have a News feed where with a simple plugin called "Simple News" i can post news items.

I have a little problem, for the activities the order needs to be 'ASC' by date. This will give the first upcoming event.

For the news page i need the oposit however. It needs to be 'DESC' in order to make the newest post appear on top.

In the Functions.php I've found the following code:

//posts ordered by post_date
function prefix_modify_query_order( $query ) {
  if ( is_main_query() ) {

    $args =  array( 'post_date' => 'DESC', 'title' => 'ASC' );

  $query->set( 'orderby', $args );
  }
 }
 add_action( 'pre_get_posts', 'prefix_modify_query_order' );

Is there an efficient way to specify the order for the posts and create an exeption for the News page?

I would love to hear

sincerely Max,

1

1 Answers

1
votes
function newspage_modify_query_order($query) {

    if (is_page('news')) { // your page slug

        if (is_main_query()) {

            $args = array('post_date' => 'DESC', 'title' => 'ASC'); // Desired order

            $query->set('orderby', $args);
        }
    }
}

add_action('pre_get_posts', 'newspage_modify_query_order');