Displaying articles alphabetically (rather then by date posted)

This support request was posted in Flatbase by lisajoy

Request ID #6362 In Progress
  • Hey, we’d like to display our articles alphabetically as we’re using a coding system to ensure our articles are in a chronological order but I’m having a bit of trouble getting this to work.

    I’ve tried adding this code to functions.php with no luck. Suggestions?

    // function and action to order articles alphabetically

    function alpha_order_classes( $query ) {
    if ( $query->is_post_type_archive(‘articles’) && $query->is_main_query() ) {
    $query->set( ‘orderby’, ‘title’ );
    $query->set( ‘order’, ‘ASC’ );
    }
    }

    add_action( ‘pre_get_posts’, ‘alpha_order_articles’ );

    I’ve tried a couple plugins like WP Post Sorting but this only seems to effect posts, not articles.

    Hello Lisa,

    Thank you for writing. Our knowledge base function have some filters in order to change the default arguments for that function.

    We’ve created a snippet in order to show how to modify the order of articles, you can have a look at it here: https://gist.github.com/juanfraa/d434bcc629647bc98393

    You can include that snippet in your functions.php file, and change the orderby value for ‘title’. You can check the full list of orderby parameters here: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Best,
    Juan.

    Thanks for sharing the snippet but for some reason it isn’t affecting the article order. If tried inserting it into flatbase/functions.php and flatbase/includes/functions.php with no luck either time. I’ve updated values to reflect title:

    ie:
    function nice_knowledgebase_article_order( $args ) {
    $args[‘orderby’] = ‘title’;
    $args[‘order’] = ‘ASC’;
    return $args;
    }
    add_filter( ‘nicethemes_knowledgebase_default_args’, ‘nice_knowledgebase_article_order’ );

    Any tips?

    Hello,

    Thank you for the follow up.

    The functions file you need to edit is the following: /flatbase/functions.php

    Please be sure that you copy the exact same code that I’ve shared with you, using the same single quotation marks. When you are in the gist page, click the “Raw” button and copy the code from there. Do not include the php opening tag.

    Best,
    Juan.

    Hi Juan,

    I’ve followed these instructions exactly with no affect on article reordering.

    See my page of articles (still displaying by date): http://wiki.kudoz.ca/article-category/culture-values

    I provided credentials initially when opening this ticket and have copied functions.php code below to demonstrate.

    *****************

    <?php
    /**
    * Flatbase by NiceThemes.
    *
    * Init NiceFramework – PLEASE DO NOT EDIT THE LINES BENEATH.
    *
    * @package Flatbase
    * @author NiceThemes
    * @license GPL-2.0+
    * @link http://nicethemes.com/theme/flatbase
    * @copyright 2014-2015 NiceThemes
    * @since 1.0.0
    */
    // If this file is called directly, abort.
    if ( ! defined( ‘WPINC’ ) ) {
    die;
    }
    // filter the knowledgebase parser

    function nice_knowledgebase_article_order( $args ){
    $args[‘orderby’] = ‘title’;
    $args[‘order’] = ‘ASC’;

    return $args;
    }

    add_filter( ‘nicethemes_knowledgebase_default_args’, ‘nice_knowledgebase_article_order’ );

    /**
    * Init NiceFramework – PLEASE DO NOT EDIT THE LINES BENEATH.
    */
    require_once ( get_template_directory() . ‘/engine/bootstrap.php’ );

    /**
    * You can add your code below =)
    */

    Hello Lisa,

    Would you please add the function below these lines:


    /**
    * You can add your code below =)
    */

    Best,
    Juan

    Hi Juan,

    The articles in the knowledge base block (in homepage template) are now displaying alphabetically but what I was requesting initially was for the category pages (archive.php template I believe) to display them alphabetically.

    Working > http://wiki.kudoz.ca/ (see knowledge block)
    Not working > http://wiki.kudoz.ca/article-category/comms

    Hi Lisa,

    Well, that’s a template that uses the auto generated query by WordPress, we haven’t modified the query there.

    For that case, you were in the right direction by using the filter for pre_get_posts. You should check that you are asking if the post type is “articles”, when it is “article”, and using that function as a method of the $query object, when it is an independent function.

    At the same time you were viewing the article category archive and not the article archive. Because of that you weren’t seeing any results. What you need to do is ask if the current page is the default taxonomy page for “article-category” (and ‘article-tag’ to make it work in every archive page).

    I’ve created a snippet for you to check: https://gist.github.com/juanfraa/cf1979da9bff55518f14

    Best,
    Juan.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

Login to your Account

Welcome back! Please log in to your account by filling the fields below:

Forgot?

Not a member? Create a free account.

Create a Free Account

You're 27 seconds away from some awesome WordPress free stuff, benefits and more. Create a free account and have access to our free products, benefits and more!

Already have an account? Log in
Secure Server