Displaying blog posts based on simple numbers
-
We are digitalizing nearly a 1000 articles that appeared in a newspaper column (say1 to 1000). We don’t have the newspaper copies, which means we can’t use the PUBLISH option in the post editor (publication dates n/a). And several people are uploading the articles available in typescripts. So, articles would be posted in any order on any given date (134, 828, 452, 15…). But we sure have one key for sorting. Luckily the author has numbered all his typescipts — 1,2,3,4,5… Kindly help. We are all beginners. Thanks.
-
Thanks, @sanau, I still have to learn a lot. Numbers are already added to the title. In fact, numbers come first even in articles. Example: 176 / Why cache is bad? 🙂 Creating index pages is beoyond me.
WP can generate index pages automatically. The default front page is an index page. If you chose to have a static front page, which ever page is selected as the post page will be the index page. One trouble is by default the posts are listed in descending date order (newest first). Since your sort numbers are the first part of every title, you’ll instead (presumably) want ascending title order, essentially alphabetical where numbers are considered as characters.
The other trouble is you need numbers to have all the same number of numerals for proper numeric sorting, which generally means numbers like 001, 002…999. If you did 1, 2 … 11, 12 … 999, then 2 would occur after 11 and 12, etc. But there’s a way to get 1, 2, 3 … 11, 12 … 999 sorted correctly, described below.
To change index page sorting, use the “pre_get_posts” action hook to alter the “order” and “orderby” query vars as desired. Only do this change for the main, non-admin query where is_archive() returns true.
To resolve the alphabetic vs. numeric sort order issue, use the “posts_request” filter to alter the actual SQL query. Add
+0to the order by title clause so that SQL orders titles as though they are numbers and not words.OMG, is it so intricate?
Ascending or descensing is no problem for us. Numbering like 001, 002 is no poblem. I think what we need is a custom field which we can fill manually when we post an article and the theme allows it. How to create that?
The WP editor has a native custom field feature. It might be hidden from your view. Use the Preferences item in the menu accessed via the 3 dot icon. In the General category, it’s under Advanced at the bottom.
TBH, the UI for this is rather clunky. It’s possible to instead use an alternative custom fields plugin, or even develop your own custom UI (assuming adequate coding skills).
You’ll still need to alter the index page’s query to order by a custom field, although the 1,2 … 11,12 problem is easier to deal with when in custom fields.
Even if you stay with ordering by titles with 001, 002, etc, the query still needs to be altered since the default is by date.
It’s also possible to alter ordering via URL query string, for example:
www.domain.com/blog/?orderby=title&order=ASC. However, some custom coding is still needed to get WP to automatically generate such links.You might try searching for a plugin that allows you to more easily alter index page ordering. I’m not aware of any, but it sounds like something that ought to be available.
www.domain.com/blog/?orderby=title&order=ASCWow! It’s really a one-of-a-kind URL to us — with no coding skills! We tried it. The sorting part works seamlessly, even amazingly, but something strange is happening as well. Before confusing you with a heap of details, let us observe it. But we can tell you what is strange about it at one glance.
For PREVIOUS/NEXT we use numbers. It is in conflict with the number of “Blog pages show at most” set under Settings>Reading. If we switch to OLDER/NEWER POSTS, there is no conflict, but the older are treated as newer and the other way round. Kindly give us some time. We don’t like to accommodate anything between a single post and the comment section.
-
This reply was modified 1 year, 6 months ago by
zebracrossing.
You’re saying that with the unusual URL, subsequent pages don’t show the correct number of posts per page? But only for numeric page links, not older/newer links? However, the “Older” link gets newer posts and vice versa?
Pagination links make certain assumptions about the nature of index and archive pages. By using order=ASC query string, we’ve altered the assumed state. The pagination links are now operating under the wrong assumptions. I’ve no explanation for posts per page being incorrect. Maybe a theme or plugin conflict?
Sadly the older/newer solution would require more coding. You essentially need more robust pagination link code to better fit with your desired index page presentation.
paginate_links()gives us the most flexibility. Unfortunately it’s difficult to use correctly. I’m a very experienced WP coder and I still don’t fully understand how to use it.Kindly give us some time.
Of course, take all the time you need. But if it takes more than a of couple weeks I recommend starting a new topic.
What we said is: The URL you gave is a Wow! The strangeness and conflict we mentioned are due slow server speed. What we change takes time to propagate.
The blog section on the front page follows published dates. We don’t have the know-how to change it, but the URL that you provided works wonderfully in displaying all the posts, regradless of the dates, if we assign it to READ MORE at the bottom of the blog excerpts of the front page.
We have decided to resort to PUBLISH on the edit page, based on an imaginary but logically flawless calendar. After all the excerpts of only 6 blog posts are displayed on the front page. The dates given to artiles 1 and 3 are two weeks apart. It’s not a tough job to use a probable date between these two. If something is wrong, we can immediately locate it, while the all-post section will follow the number in the article title. In short, your URL helped us solve our problem. We know what to do now. Thanks for your valuable time spent for us.
-
This reply was modified 1 year, 6 months ago by
The topic ‘Displaying blog posts based on simple numbers’ is closed to new replies.