Search code examples
wordpresssortingcategoriesposts

Wordpress - Sort post of one category alphabetically


I'm at the end, I'm not even sure if I'm doing the right thing. I added the current code to the function.php, I need to change the order for one specific category. So I mean to say the category "internal-communication" to display posts by date added or alphabetically, but only one, I set a different global setting for the others. Can anyone help me? Thank you.

$args = array(
    'posts_per_page'   => 5,
    'offset'           => 0,
    'category'         => 'interni-sdeleni',
    'orderby'          => 'post_date',
    'order'            => 'ASC',
    'include'          => 'interni-sdeleni',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => 'interni-sdeleni',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true );

$posts_array = get_posts( $args );

Solution

  • Try with this code -

    $args = array(
    'posts_per_page'   => 5,
    'offset'           => 0,
    'category'         => 'interni-sdeleni',
    'orderby'          => 'name',
    'order'            => 'ASC',
    'include'          => 'interni-sdeleni',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => 'interni-sdeleni',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true );
    

    $posts_array = get_posts( $args );