Php 在mysql中选择相邻行–但使用特定列的字母顺序,而不是ID的数字顺序时
我有一个wordpress安装,带有自定义的post类型,我希望通过mysql找到“previous”和“next”自定义post类型对象 关于如何在使用ID时实现这一点,我已经阅读了很多关于SO的问题/答案。问题是,这些对象中有很多是在不同的时间输入的,因此它们的数字“ID”顺序不一定与它们的字符串post_title顺序对应 例如: SQL查询:Php 在mysql中选择相邻行–但使用特定列的字母顺序,而不是ID的数字顺序时,php,mysql,sql,wordpress,Php,Mysql,Sql,Wordpress,我有一个wordpress安装,带有自定义的post类型,我希望通过mysql找到“previous”和“next”自定义post类型对象 关于如何在使用ID时实现这一点,我已经阅读了很多关于SO的问题/答案。问题是,这些对象中有很多是在不同的时间输入的,因此它们的数字“ID”顺序不一定与它们的字符串post_title顺序对应 例如: SQL查询: SELECT ID, post_title FROM wp_posts WHERE post_type = 'custom_type' OR
SELECT ID, post_title
FROM wp_posts
WHERE post_type = 'custom_type'
ORDER BY post_title ASC
给我结果。如果我从上面知道ID,例如b,1,我怎么会发现,当按post_title排序时,相邻的post是7和3?您可以使用union all执行此操作:
如何按CPT中相邻帖子的字母顺序排序。可能比较干燥,但这是其他溶液的1/4
add_action( 'wp_head', 'projectprefix_adjacent_post_equipment_filters' );
/**
* Sort Adjacent Posts (previous and next) in CPT alphabetically
* get_previous_post_join, get_next_post_join
* get_previous_post_where, get_next_post_where
* get_previous_post_sort, get_next_post_sort
* Ref: https://wordpress.stackexchange.com/a/73194/64742
*/
function projectprefix_adjacent_post_equipment_filters() {
// exit early
if ( ! is_singular( array( 'registered_cpt' ) ) ) return;
add_filter( 'get_previous_post_where', 'projectprefix_prev_post_where' );
function projectprefix_prev_post_where( $where ) {
global $post, $wpdb;
$where = $wpdb->prepare( "WHERE p.menu_order > '%s' AND p.post_type = 'registered_cpt' AND p.post_status = 'publish'",$post->menu_order );
return $where;
}
add_filter( 'get_next_post_where', 'projectprefix_next_post_where' );
function projectprefix_next_post_where( $where ) {
global $post, $wpdb;
$where = $wpdb->prepare( "WHERE p.menu_order < '%s' AND p.post_type = 'registered_cpt' AND p.post_status = 'publish'",$post->menu_order );
return $where;
}
add_filter( 'get_previous_post_sort', 'projectprefix_previous_post_orderby' );
function projectprefix_previous_post_orderby( $orderby ) {
return "ORDER BY p.menu_order ASC LIMIT 1";
}
add_filter( 'get_next_post_sort', 'projectprefix_next_post_orderby' );
function projectprefix_next_post_orderby( $orderby ) {
return "ORDER BY p.menu_order DESC LIMIT 1";
}
}
可能的重复项没有post_日期字段,为什么不能按日期字段排序?@BrianDeMilia原因是我想要基于字母顺序的prev/next,而不是日期。这不太管用,但这个,以及上面的相关问题确实帮助我找到了解决方案,我没有意识到mysql可以使用字符串
(SELECT ID, post_title
FROM wp_posts
WHERE post_type = 'custom_type' and
post_type < (select post_title from wp_posts where post_type = 'custom_type' and id = 1)
ORDER BY post_title DESC
LIMIT 1, 1
)
UNION ALL
(SELECT ID, post_title
FROM wp_posts
WHERE post_type = 'custom_type' and
post_type > (select post_title from wp_posts where post_type = 'custom_type' and id = 1)
ORDER BY post_title ASC
LIMIT 1
)
ORDER BY ID;
add_action( 'wp_head', 'projectprefix_adjacent_post_equipment_filters' );
/**
* Sort Adjacent Posts (previous and next) in CPT alphabetically
* get_previous_post_join, get_next_post_join
* get_previous_post_where, get_next_post_where
* get_previous_post_sort, get_next_post_sort
* Ref: https://wordpress.stackexchange.com/a/73194/64742
*/
function projectprefix_adjacent_post_equipment_filters() {
// exit early
if ( ! is_singular( array( 'registered_cpt' ) ) ) return;
add_filter( 'get_previous_post_where', 'projectprefix_prev_post_where' );
function projectprefix_prev_post_where( $where ) {
global $post, $wpdb;
$where = $wpdb->prepare( "WHERE p.menu_order > '%s' AND p.post_type = 'registered_cpt' AND p.post_status = 'publish'",$post->menu_order );
return $where;
}
add_filter( 'get_next_post_where', 'projectprefix_next_post_where' );
function projectprefix_next_post_where( $where ) {
global $post, $wpdb;
$where = $wpdb->prepare( "WHERE p.menu_order < '%s' AND p.post_type = 'registered_cpt' AND p.post_status = 'publish'",$post->menu_order );
return $where;
}
add_filter( 'get_previous_post_sort', 'projectprefix_previous_post_orderby' );
function projectprefix_previous_post_orderby( $orderby ) {
return "ORDER BY p.menu_order ASC LIMIT 1";
}
add_filter( 'get_next_post_sort', 'projectprefix_next_post_orderby' );
function projectprefix_next_post_orderby( $orderby ) {
return "ORDER BY p.menu_order DESC LIMIT 1";
}
}