• I’m trying to add a sort function – where an admin can enter a number in a box to sort the listings.

    I’ve looked for some solutions, but just am not finding it. I’m thinking it’s tied in with meta-box and function.php

    Thanks for your input…

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Your thinking is correct. I believe your intent is this number is entered for each post on the post edit screen, and that number is used to determine the sort order when the posts are displayed on the various home and archive listings instead of the default published date. Correct?

    You can place a meta box on the edit screen. Then you would hook the ‘pre_get_posts’ action to alter the query to order by meta_value_num. All this code can go on functions.php, but it could also be made into a simple plugin. If you place it on functions.php, you should create a child theme to contain your code to protect it from being lost during theme updates. Creating a child theme or creating a plugin are equally easy to do.

    See these references for altering a query’s order by parameters:
    Plugin API/Action Reference/pre get posts
    Class Reference/WP Query#Order & Orderby Parameters

    And for adding meta boxes:
    https://developer.wordpress.org/plugin/metadata/creating-custom-meta-boxes/

    There are a few references to plugins there, but all the techniques can be implemented in a child theme just as well.

Viewing 1 replies (of 1 total)

The topic ‘Adding a sort function by user entered numbers’ is closed to new replies.