Displaying articles alphabetically (rather then by date posted)
This support request was posted in Flatbase by lisajoy
- lisajoy September 25, 2015 at 10:43 pm
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’ );
lisajoy September 25, 2015 at 11:06 pmI’ve tried a couple plugins like WP Post Sorting but this only seems to effect posts, not articles.
Juanfra Aldasoro September 26, 2015 at 2:34 pmHello 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.lisajoy September 26, 2015 at 9:18 pmThanks 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?
Juanfra Aldasoro September 27, 2015 at 2:41 pmHello,
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.lisajoy September 27, 2015 at 7:41 pmHi 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 parserfunction 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 =)
*/Juanfra Aldasoro September 28, 2015 at 2:51 pmHello Lisa,
Would you please add the function below these lines:
/**
* You can add your code below =)
*/
Best,
Juanlisajoy September 28, 2015 at 3:58 pmHi 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/commsJuanfra Aldasoro September 28, 2015 at 9:11 pmHi 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.
You must be logged in to reply to this topic.