PostgreSQL for WordPress (PG4WP)
WP_Query Order By Meta_Value_Num (1 post)

  1. andrewmunsell
    Posted 3 years ago #

    For those using a WP_Query to order posts by a custom meta_value (numerically, using 'orderby' => 'meta_value_num'), you will have to make a change to the driver_pgsql.php file. Because WordPress creates a query casting the string to an integer (that only works in MySQL), this has to be rewritten for PgSQL.

    To do this, in driver_pgsql.php at line 288, add:

    if(preg_match('/wp_postmeta\.meta_value\+0/', $sql))
            $sql = str_replace('wp_postmeta.meta_value+0', 'cast(wp_postmeta.meta_value as int)', $sql);

    This converts the MySQL cast (the +0) to the PgSQL cast() function.

    Hopefully this will help someone else who's struggling with this in the future.


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • PostgreSQL for WordPress (PG4WP)
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic


No tags yet.