Latest Articles display order

I am generating a list of the latest 4 articles, and want to sort them by date published. This is in order to display the last 4 articles that were published on the site. The problem I am having is that the articles are being displayed in the order determined by the submission_id and not the date published.

This is causing earlier articles to be displayed before later articles, if they have a higher submission_id value.

The code I am using to generate the articles list is as follows:

$request = Application::get()->getRequest();
		$journal = $request->getJournal();
		import('classes.submission.Submission'); // import STATUS_ constants

			//$submissionsIterator = Services::get('submission')->getMany(['contextId' => $journal->getId(), 'status' => STATUS_PUBLISHED, 'count' => RCVSK_LATEST_ARTICLES_DEFAULT]); // this is the original line
			$submissionsIterator = Services::get('submission')->getMany(['contextId' => $journal->getId(), 'status' => STATUS_PUBLISHED, 'count' => RCVSK_LATEST_ARTICLES_DEFAULT, 'orderBy' => 'date_published','orderDirection' => 'DESC']);  // This is the modified line
			$submissionsInSections = [];
			foreach ($submissionsIterator as $submission) {
				$submissionsInSections[]['articles'][] = $submission;
			}

When the articles are being displayed it shows them in the order of submission_id and not date_published.

How would I modify the query so that it actually does grab the latest articles by date_published?

I am using OJS 3.3.0.13 (latest)

As far as I can tell, all publications need to be scanned first and then all submissions within those publications need to be scanned and sorted by date. This is because it is possible to have articles with higher internal ID numbers but earlier published dates depending on how long the article was in an unpublished state.

The resulting set of article records does not necessarily contain the most recent chronological articles unless all publications are scanned first, which is computationally expensive and takes time to generate the page.

Isn’t there a “cheap” way to collate the 4 most recent articles in chronological order from all publications? I have been looking for examples but haven’t found any specific to this.

Ignore this! I was being a bit too complicated… the final solution is just:

	/* retrieve latest articles */
		$publishedArticleObjects = Services::get("submission")->getMany([
			'status' => STATUS_PUBLISHED,
			'contextId' => $journalId,
			'count' => LATEST_ARTICLES_DEFAULT,
			'orderBy' => 'datePublished',
		]);

		$smarty->assign('publishedArticles', iterator_to_array($publishedArticleObjects));
1 Like

This topic was automatically closed after 9 days. New replies are no longer allowed.