How to display a column with an arbitrary field in a post and sort by it

Article with an asterisk, for those who are already working on “How to display a column with an arbitrary field in a record and sort by it”

Often we make custom fields and custom types that are needed for development, but sometimes you need to please the client and configure the display of this field in a separate column of the admin panel, and even better if the client can sort this column by views and do his own traffic analysis, without going to google analytics or metrics.

How to make an extra column in the list of WordPress posts and set up sorting by it.

To get started, you will need the manage_ filter{$screen->id}_sortable_column instead of $screen->id you will need to specify the name of the data type, below you will see an example of this.

The filter passes an array with the names of the columns to be sorted as keys, and output data as value. The “orderby” parameter specifies, which columns will be sorted. You can also disable sorting for certain "unsortable" columns.

It will be clearer to consider everything using the example of creating a column

Filter manage_posts_columns calls a function posts_column_views() which creates a Views column for the post data type, the data type must be correctly specified in the name of the manage_ function{post_type}_columns.

Action manage_posts_custom_column calls a function posts_custom_column_views() which is responsible for filling the new column with data. In the name of the action, we can also see the specified data type manage_{post_type}_custom_column. We can see that in the fill function, another function is called which passes the getPostViews data to it.().

Consider the getPostViews function()

The function defines 2 variables $count_key and $count. In the $count_key variable, we put the name of the future custom field that will store the number of page views. We get the old content of the custom field into the $count variable.

If we received an empty value in the $count variable, or it most likely simply does not exist. If the variable is empty, then we create a custom field for the current post and put the null value “0 View” into it.

A feature that increases the view rate by 1 when viewing a page

Further, in order to add new values, it would be great to put in the template code, to single.php file, since it is he who is responsible for calling the function in posts of type Post.

At the beginning of the function we declare variables, talked about before, so what is stored in them read above.

Further, if the counter is not created, the function creates it and puts the first value in an arbitrary field of the counter. If the $count variable is not empty, then we take its value and increase it by 1.

If you do it all, you will be able to see the new column in the posts section, in the admin panel with the number of views, it will be filled if the post had views.

Adding sorting to a column in WordPress

Next, we will look at a code example for this case., but if you read and understand, you can add sorting for any column.

Filter manage_edit-post_sortable_columns indicates that our column can be sorted. I've added a commented line of code below., with which you can, on the contrary, disable sorting of the desired line.

In action pre_get_posts i call the function my_view_orderby(). Let's look at it line by line.

  1. First we check if the user is logged in.
  2. If the user is logged in, I give parameter request “orderby”.
  3. If the "orderby" parameter returned the name of our column 'post_views', then we indicate by which arbitrary field to sort and that moment, that sorting should be by numeric data.

Try it and write about the achievements in the comments, it would be interesting to know your opinion about the lesson. This is how it happened to me.

Как вывести в записи колонку с произвольным полем и сделать по ней сортировку


Buy WordPress hosting
/* WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal