Skip to content

Instantly share code, notes, and snippets.

@kierzniak
Last active June 15, 2018 10:57
Show Gist options
  • Select an option

  • Save kierzniak/42b119043a95f8ba7b6aff7dbb26992e to your computer and use it in GitHub Desktop.

Select an option

Save kierzniak/42b119043a95f8ba7b6aff7dbb26992e to your computer and use it in GitHub Desktop.

Revisions

  1. kierzniak revised this gist Jun 15, 2018. No changes.
  2. kierzniak revised this gist Jun 15, 2018. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,9 @@ function motivast_wp_query() {
    new WP_Query(array(
    /**
    * This line will not modify your query but we can use it to
    * identify query later `$wp_query->get('order_by') === 'custom'`.
    * identify query later `$wp_query->get('orderby') === 'custom'`.
    */
    'order_by' => 'custom'
    'orderby' => 'custom'
    ));
    }

    @@ -24,17 +24,17 @@ function motivast_wp_query() {
    *
    * @return string
    */
    function motivast_order_by_custom_column( $orderby, $wp_query ) {
    function motivast_orderby_custom_column( $orderby, $wp_query ) {

    /**
    * If WP_Query has `custom` as order_by parameter replace SQL
    * If WP_Query has `custom` as orderby parameter replace SQL
    */
    if( $wp_query->get('order_by') === 'custom' ) {
    if( $wp_query->get('orderby') === 'custom' ) {
    return 'wp_posts.custom DESC';
    }

    return $orderby;

    }

    add_filter( 'posts_orderby', 'motivast_order_by_custom_column', 10, 2 );
    add_filter( 'posts_orderby', 'motivast_orderby_custom_column', 10, 2 );
  3. kierzniak revised this gist Jun 13, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@ function motivast_wp_query() {

    new WP_Query(array(
    /**
    * This line will not modify your query but we can use to identify
    * query later `$wp_query->get('order_by') === 'custom'`.
    * This line will not modify your query but we can use it to
    * identify query later `$wp_query->get('order_by') === 'custom'`.
    */
    'order_by' => 'custom'
    ));
  4. kierzniak revised this gist Jun 13, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,10 @@
    function motivast_wp_query() {

    new WP_Query(array(
    /**
    * This line will not modify your query but we can use to identify
    * query later `$wp_query->get('order_by') === 'custom'`.
    */
    'order_by' => 'custom'
    ));
    }
  5. kierzniak revised this gist Jun 13, 2018. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,6 @@ function motivast_wp_query() {
    /**
    * Filters the ORDER BY clause of the query.
    *
    * @since 1.5.1
    *
    * @param string $orderby The ORDER BY clause of the query.
    * @param WP_Query $wp_query The WP_Query instance (passed by reference).
    *
  6. kierzniak created this gist Jun 13, 2018.
    38 changes: 38 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    <?php

    /**
    * Make sample fake query to show working example
    */
    function motivast_wp_query() {

    new WP_Query(array(
    'order_by' => 'custom'
    ));
    }

    add_action( 'wp', 'motivast_wp_query' );

    /**
    * Filters the ORDER BY clause of the query.
    *
    * @since 1.5.1
    *
    * @param string $orderby The ORDER BY clause of the query.
    * @param WP_Query $wp_query The WP_Query instance (passed by reference).
    *
    * @return string
    */
    function motivast_order_by_custom_column( $orderby, $wp_query ) {

    /**
    * If WP_Query has `custom` as order_by parameter replace SQL
    */
    if( $wp_query->get('order_by') === 'custom' ) {
    return 'wp_posts.custom DESC';
    }

    return $orderby;

    }

    add_filter( 'posts_orderby', 'motivast_order_by_custom_column', 10, 2 );