WordPress knowledge sharing

Modify the default sorting method of WordPress articles

When we use WordPress to publish articles, we will use a function that we want to place a specific article or several articles on the top of the home page, instead of WordPress's default ranking of articles in descending order according to the publishing time, that is, articles are ranked first, second, third, fourth, etc. according to my ideas. Although some topics provide the top setting function, it still cannot meet the requirement of completely customizing the sorting of articles. The default wp blog does not provide this function, but it can also be implemented using plug-ins. Based on the principle of no plug-in, Weieis Blog will talk about how to Modify the default sorting of wordpress articles , get rid of the method of ascending and descending order of release time.

First, add one. In the WordPress background "Write an article", click "Display options" at the top and check "Custom columns" at the top.

If you use the Gutenberg editor provided with WordPress, in the article editing status, click the three vertical points in the upper right corner to enter Preferences>Panel>Extra>Custom Fields.

At this time, you will see the user-defined column below the article editing area. Generally, Lao Wei is used to dragging this column to the top of the right side with the left mouse button, so that he can directly enter the sequence number when editing an article.

Click to enter a new column, enter post_order, the value is 0, click to add a user-defined column, and the result is as shown in the following figure. In this way, a custom column is added. The initial value is 0. This post_order is used for sorting.

This method You need to set the sorting value for each article This is how the ranking of top articles on the homepage of Weieis blog is set. In the future, each time you write an article, enter a large enough number in the value field. For example, the setting of Old Wei started from 00001, which means that it can write 10000 articles. According to the speed of writing one article every day, it can be used to rank articles that are enough to write for ten years. If it is not enough, you can add several zeros to the front.

Download the index.php file in your website template and make a backup. Replace the following code in index.php, change the default release time sorting to your meta_key, and add a meta_key. Find the following code
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args=array(
The following code starts here

‘caller_get_posts’ => 1,
‘paged’ => $paged

replace with

‘meta_key’ => ‘post_order’,
‘orderby’ => ‘meta_value’,
‘paged’ => $paged,
‘order’ => ‘DESC’

);
After uploading the overwritten source file, you can sort the articles according to your defined meta_data.

Finally, this method is not only applicable to the home page, but also applicable to the tab page, category page and other archive pages. The specific modification method is the same as the article page. You can try it yourself.

Like( zero )
Article name: Modify the default sorting method of WordPress articles
Article link: https://www.vpsss.net/1709.html
Copyright notice: The resources of this website are only for personal learning and exchange, and are not allowed to be reproduced and used for commercial purposes, otherwise, legal issues will be borne by yourself.
The copyright of the pictures belongs to their respective creators, and the picture watermark is for the purpose of preventing unscrupulous people from stealing the fruits of labor.