定制 carawebs/wp-organise-posts 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

carawebs/wp-organise-posts

Composer 安装命令:

composer require carawebs/wp-organise-posts

包简介

Set the display order of WordPress Custom Post Types by drag and drop on the posts edit screen. Ordering is achieved by an index that is set when you drag and drop them in place.

README 文档

README

Set the display order of Custom Post Types by drag and drop on the posts edit screen. Ordering is achieved by an index that is set when you drag and drop them in place.

Build Notes

A post meta value is used to order projects on project-category taxonomy archive pages.

It was tricky to create a modified loop that displayed:

  • FIRST: projects with the relevant post meta key set, in the correct order as determined by the value
  • SECOND: projects without the key

The key value pairing will be added after sorting, so it is important that all projects are shown, even if they don't have the key.

<?php
/**
* 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' ] );

}

This simple query returns all posts with the key, by value order:

<?php
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']);

}
<?php
// query in old format
$query->set( 'meta_query', array(
    'relation' => 'OR',
    array( 'key' => $key, 'compare' => 'EXISTS' ),
    array( 'key' => $key, 'compare' => 'NOT EXISTS' )
    ) );
    $query->set( 'orderby', [ $key => 'ASC', 'date' => 'DESC' ] );

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2017-02-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固