PHP code example of carawebs / wp-organise-posts

1. Go to this page and download the library: Download carawebs/wp-organise-posts library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

carawebs / wp-organise-posts example snippets


/**
* Set a custom order for posts
*
* This is a callback for the `pre_get_posts` filter hook
*
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
* @param  object $query The $query object - passed by reference.
* @return void
*/
public function custom_order ( $query ) {

    // Get the term that is being displayed for the given custom taxonomy
    $this_term = $query->query['project-category'];

    // Standardised key for post meta
    $key = "project-category-$this_term";

    $query->set( 'meta_query', [
        'relation' => 'OR',
        [ 'key' => $key, 'compare' => 'EXISTS' ],
        [ 'key' => $key, 'compare' => 'NOT EXISTS' ]
    ]);

    $query->set( 'orderby', [ $key => 'ASC', 'date' => 'DESC' ] );

public function custom_order ( $query ) {

    // Get the term that is being displayed for the given custom taxonomy
    $this_term = $query->query['project-category'];

    // Standardised key for post meta
    $key = "project-category-$this_term";

    // Set the meta_key and orderby it's value
    $query->set('meta_key', $key);
    $query->set('orderby', ['meta_value' => 'ASC', 'date' => 'DESC']);