I have created an event diary that only uses custom fields for each article to enable a standard display format (location, date, time, etc).
(Hopefully) I will not be adding these events myself but will have a volunteer from the site owner's organisation to do this. There is a field for date and time which will display in the format yyyy-mm-dd hh:mm and I would like to order the articles using that field. That would then ensure that the events are displayed in the order in which they will take place. At the moment, I have set up some dummy articles to demonstrate this new feature to the site owner but I have added the date and time to the article title, e.g. "yyyy mm dd hh mm event name", so that the articles are ordered by title. However, I can pretty much guarantee that the volunteer editors will probably forget to do this and just add the event name, so I'd like to automate it as much as possible.
Is this possible?
How to order articles by field value Topic is solved
Moderators: ooffick, General Support Moderators
Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
-
- Joomla! Explorer
- Posts: 396
- Joined: Wed Oct 01, 2014 10:25 am
How to order articles by field value
Last edited by toivo on Thu Mar 09, 2023 4:05 am, edited 1 time in total.
Reason: mod note: moved from 4.x Administration
Reason: mod note: moved from 4.x Administration
- toivo
- Joomla! Master
- Posts: 17487
- Joined: Thu Feb 15, 2007 5:48 am
- Location: Sydney, Australia
Re: How to order articles by field value
Possible but it requires a bit of development. The class ArticlesModel can be extended so that the protected function ArticlesModel::getListQuery can be overridden, in other words, modified without altering the Joomla core. The purpose of the modification is to include the custom field in the SQL query that the category list is based on.
The technique how to override a function by extending its class is discussed in the 2021 forum topic extension override in joomla 4.
The proof of concept (POC) uses a custom field MyDate as the sort argument for the Category List. A modified version of components/com_content/src/Model/ArticlesModel.php of Joomla 4.2.8 is attached.
The modified and added lines are commented with today's date // 20230309, for example:This script is only an example for further testing and development of a proper plugin that extends the class ArticlesModel and overrides the method getListQuery().
The technique how to override a function by extending its class is discussed in the 2021 forum topic extension override in joomla 4.
The proof of concept (POC) uses a custom field MyDate as the sort argument for the Category List. A modified version of components/com_content/src/Model/ArticlesModel.php of Joomla 4.2.8 is attached.
The modified and added lines are commented with today's date // 20230309, for example:
Code: Select all
// 20230309 start
$db->quoteName('v.value', 'mydate'),
// 20230309 end
You do not have the required permissions to view the files attached to this post.
Toivo Talikka, Global Moderator
-
- Joomla! Explorer
- Posts: 396
- Joined: Wed Oct 01, 2014 10:25 am
Re: How to order articles by field value
Well, taking 11 months to respond to all your hard work is not good enough and I apologise profusely.
I'm now back to this, but using J5.
It works (thank you) when I put the code in the Joomla Core, replacing /components/com_content/src/Model/ArticlesModel.php. Obviously, that's not the right thing to do, but I'm not sure where this file should go - or should it have a new, unique name in the same location?
Whilst the articles are now sorted by the custom field date (hooray!) I would like to limit them to articles where the custom field date is in the future. In my test site I have four articles, one of which has a custom field date in the past. I modified your code as below but nothing changed and I still see four articles.
Code: Select all
$query->where($db->quoteName('v.id') . ' = "1"' AND $db->quoteName('v.value') . ' > now()');
I have copied the basic J5 fields.xml file as a start and tweaked it:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<extension type="plugin" group="content" method="upgrade">
<name>plg_content_events</name>
<author>Joomla! Project</author>
<creationDate>2024-02-13</creationDate>
<copyright>(C) 2017 Open Source Matters, Inc.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>[email protected]</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>5.0.2</version>
<description>Events Blog</description>
<namespace path="src">Joomla\Plugin\Content\Events</namespace>
<files>
<folder plugin="events">services</folder>
<folder>src</folder>
</files>
<config>
<fields name="params">
<fieldset name="basic">
</fieldset>
</fields>
</config>
</extension>
-
- Joomla! Explorer
- Posts: 396
- Joined: Wed Oct 01, 2014 10:25 am
Re: How to order articles by field value
UPDATE: Yesterday I discovered that Nicholas and Crystal Dionysopoulos have already written a plugin to sort by field. See https://github.com/nikosdion/sortbyfield
It does exactly what I asked for in my original post.
I'm still working on the pre-filtering by <today's date.
It does exactly what I asked for in my original post.
I'm still working on the pre-filtering by <today's date.