WP Meta Sort Posts WordPress Plugin

Help me continue to develop this plugin:

I initially developed WP Meta Sort Posts plugin because I needed to be able to create archive-style pages for posts based on custom fields. I quickly realized that it would be just as easy to knock down all the walls and allow for custom sorted pages to be created based on any combination of WordPress query variables. WP Meta Sort Posts is still in its initial beta release and I welcome all feedback and feature suggestions. If you would like to participate in development I will be managing the project through

Installation Instructions

  1. Download the “.zip” file and upload the wp-meta-sort-posts folder and its contents to your WordPress /wp-content/plugins directory.
  2. Visit your WordPress Admin Dashboard and activate WP Meta Sort Posts in the “Plugins” menu.
  3. Set the plugin options in the Admin Dashboard “Settings” menu under “WP Meta Sort Posts Options.”


WP Meta Sort Posts Plugin Options


Theme’s loop file

The best way to mimic your theme’s look and feel is to use your theme’s original loop to present the posts. Not all themes will have its loop broken out. If it is, you can enter the name of the file excluding the “.php” file extension in the WP Meta Sort Posts options page under “Settings” in your WordPress admin dashboard. In some cases your themes loop file will actually be called “loop.php,” however, it is not a strict standard and can be named anything. For an example of what a loop file will look like, see The Loop.

If your theme does not have have a loop file, you can use the basic loop built into the plugin. Right now the included loop is very basic, but it works. Adding more robust configuration options to the included loop is the top priority for a future release.

If you would like help identifying the best options for your theme, please leave a request in the comments below.


Page Navigation Location

Top. Sets the page navigation above the list of posts.

Bottom. Sets the page navigation below the list of posts.

Both. Sets the page navigation above and below the list of posts.

None. Does not include page navigation. This option is used when the theme’s loop file already includes navigation.


WP Meta Sort Posts Plugin Shortcode Usage

Create a new page and include a shortcode in the following format:


[msp query_string=”QUERY”]

“QUERY” in the above example refers to a complete query in URL Query String format. Both public and private WordPress query variables can be passed as long as it is formatted appropriately, e.g.


[msp query_string=”meta_key=shortcode_test&meta_value=Arizona&orderby=meta_value&order=asc”]

Notes: Do not URL Encode special characters like spaces to %20. Do not use query variables “paged” and “offset” in your shortcode because they are automatically calculated and added to the query string.

If you prefer, you can also pass each argument separately, e.g.


[msp Argument1=”Value1” Argument2=”Value2” Argument3=”Value3”]

The same query will be performed as in the first example if the arguments are passed like this.


[msp meta_key=”msp_test” meta_value=”Arizona” orderby=”meta_value” order=”asc”]


Supported Themes

This plugin was initially developed for HeatMap Theme Pro, but also includes preconfigured options for Twenty Ten and can be used with any theme by setting the theme’s loop file option to “MSP” in order to use the provided loop. As I receive requests for help with specific themes I will add support for those in future releases of the plugin and will add them to the following list.



    So, I’ve used the plugin, and I have a custom field called “price” which has prices entered in a decimal format, like 199.99, 89.99, etc. But oddly they do not sort in true ascending or descending order? Any idea why?

    Ultimately I’m trying to add some functionality to this website so that a visitor can sort the products based on brand or price within a specific category. I’ve been searching and searching for things to try, and yours seems most likely to succeed so far, although as far as I can tell I will have to create many additional WP pages to get this to work, for instance at least two for every category, one for ascending and one for descending sorting, and that would be if the only sortable item I handled is price. If I also do brand, that would be 4 pages per category.

    I am not educated in PHP or Javascript, so I know that there are likely easier ways to accomplish this.

    So, two questions:

    1) Do you know why the items in the page linked above aren’t sorting correctly?

    2) So you know of an easy way to click in the page to, for instance, switch the sort from asc to dsc, or from one meta_key to another?


    • Jason Pitts says:

      I am sorry it has taken me so long to respond. I have not been paying much attention to my Web presence recently. I looked at your site and it doesn’t look like you have found a solution yet. If you would still like to implement this, use the contact form and send me the shortcode you were using, along with the some of the meta values from 3 or 4 random posts. I will see what I can do for you.

  2. This is a really useful tool, but I’m trying to create a page where I have multiple queries listed with a topical header


    Header One

    [msp QUERY1]

    Header 2

    [msp QUERY2]

    However, the resulting page is listing all the query results together and dumping the headers at the bottom of the page. I tried to fix this with DIVs, but I think it has to do with how the hook works, and I’m no expert so I have no idea of how to modify it….

    Do you think you might be able to modify this in the future to work so that a page could contain MSP queries inline with page content? thanks.

    • Jason Pitts says:

      I will definitely keep this suggestion in mind. I have some other plans for this plugin as well, but have been very busy recently, so I havn’t been able to work on it. I expect things will slow down in the next month or two and then I can start working on the next version.

  3. Using White Gold, I don’t understand how this plugin works. Is it supposed to give an ordered list of posts? It looks like the theme automatically puts each post on a different page…maybe not compatible?

    • Jason Pitts says:

      This plugin will query and sort posts by any fields stored in your database. If it is only showing one post per page it probably has to do with either your WordPress settings or more likely the way the query in your shortcode is written. Can you give me the shortcode you are using, a description of what you want it to do and a link to the blog you are trying to use it on? I am working 14 hour days right now, so it may be late before I can get back to you.

  4. Can this be used with dates and/or timestamps?

    • Jason Pitts says:

      Hi Jenny, Thanks for asking!

      You can definitely use this to query dates. As for timestamp, I am really not sure. There are a lot of WordPress query variables that are not listed in the query vars codex page, but as you can see, there are many time-oriented arguments that can be passed. If you want to tell me how exactly you want to pull your posts, maybe I can help you write your query string.

      • that would be great!
        “ad_cat”= “display_ad”,
        cp_sys_expiredate= “this thursday” or 07/05/2012 00:00:00 the cp_sys_expiredate is a timestamp

        I can’t get into the files to change it because it is a local website that I maintain from my parent company. In fact for that matter I can’t get several plugins to work because of the customization that they gave me. Thank You so much

        • Jason Pitts says:

          It looks like your trying to query a timestamp stored in a custom field. The WordPress query variables would be looking at publish date, in that case you could do something like this:

          [msp post_type=”ad_listing” ad_cat=”display_ad” monthnum=”7” day=”05” year=”2012”]

          but in your case you would have to manually type in the timestamp like this:

          [msp post_type=”ad_listing” ad_cat=”display_ad” cp_sys_expiredate=”1341446400”]

          Are you doing this to try and get a list of ads that are expiring in the future? If so you could mod wp-meta-sort-posts.php to handle a custom argument pretty easily. Just insert the following after the line labled “ /* Handle shortcode Arguments */”:

          if (isset($args['cp_sys_expiredate'])){
          $args['cp_sys_expiredate'] = time() + 86400;

          in that case no matter what value you set for cp_sys_expiredate, as long as it is set, will query posts with an expire date of exactly 24 hours from the time the page is viewed. You will probably need to massage the code a bit to make it work exactly how you want it.

          • Thank You so much. My goal is to find all ads(posts) that are set to expire on the thursday of each week, so I can modify them if they clients decides to run them an additional week or more.

            Can you elaborate on the last part of your response? I modified the plug in but am unsure why I need to find something 24 hours after a page is viewed, is that what that number is in the if statement? 24 hours. We are a relatively new site and am not sure if all the ads are viewed yet. I have around 1000 ads each week.

            You are awesome for helping me

            • Jason Pitts says:

              I apologize for not being clear. The previous mod will show you all posts with a cp_sys_expiredate of exactly 24 hours from the point in time that you view the list of posts. in other words it is calculating current timestamp + 86,400 seconds (24 hours) and looking for posts with timestamps in cp_sys_expiredate that match that exact number.

              Are the timestamps for your ad’s always set to be “this thursday” 00:00:00 or is the time of day variable?

              • the time of day i need is/will be static at 00:00:00, This is what I have it set to. I did reply to your email a couple hours ago. I hope you got it.

                • Jason Pitts says:

                  Well, I think I got you covered 🙂 Just replace the previous moded code with this:

                  if (isset($args['cp_sys_expiredate'])){ //if cp_sys_expiredate is passed.

                  $dt=explode(':',date('d:m:Y',time())); //convert current day month year to array
                  $midnight_today = mktime(0,0,0,$dt[1],$dt[0],$dt[2]); //make timestamp of midnight current day using array

                  switch (Date ("D")) //Date ("D") retrieves the first three letters of the current day.
                  case "Sun":
                  $next_thurs = $midnight_today + (4 * 86400); //4 days till thursday so 4 times 86400 seconds (24 hours) = Next thursday at midnight.
                  case "Mon":
                  $next_thurs = $midnight_today + (3 * 86400); //and so on...
                  case "Tue":
                  $next_thurs = $midnight_today + (2 * 86400);
                  case "Wed":
                  $next_thurs = $midnight_today + (1 * 86400);
                  case "Thu":
                  $next_thurs = $midnight_today + (7 * 86400); //replace with $next_thurs = $midnight_today if you want to see ads that expired on the current thursday.
                  case "Fri":
                  $next_thurs = $midnight_today + (6 * 86400);
                  case "Sat":
                  $next_thurs = $midnight_today + (5 * 86400);
                  $args['cp_sys_expiredate'] = $next_thurs;

                  • so that wasn’t it what am I doing wrong?

                    • Jason Pitts says:

                      Just to close out this comment thread I thought I would update. It turned out that the values were being held in a standard date/time format in a normal custom field, so with a slight tweak to the above code and a page with a short code querying the meta_key and meta_value made it work.

Speak Your Mind