您可以尝试以下操作:
$query = new WP_Query(
[
\'wpse_pid\' => 100, // Our custom post id argument
\'wpse_compare\' => \'>\', // Out custom compare argument (<,>,<=,>=,!=,<>)
\'posts_per_page\' => 100, // Modify this to your needs
]
);
我们使用以下插件来支持这两个自定义参数:
<?php
/**
* Plugin Name: Support for post ID filtering in WP_Query
* Description: Uses wpse_pid and wpse_compare arguments
* Plugin URI: http://wordpress.stackexchange.com/a/193415/26350
*/
add_filter( \'posts_where\', function( $where, $q )
{
global $wpdb;
if( $pid = $q->get( \'wpse_pid\' ) )
{
// Get the compare input
$cmp = $q->get( \'wpse_compare\' );
// Only valid compare strings allowed:
$cmp = in_array(
$cmp,
[ \'<\', \'>\', \'!=\', \'<>\', \'<=\', \'>=\' ]
)
? $cmp
: \'=\'; // default
// SQL part
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID {$cmp} %d ", $pid ) ;
}
return $where;
}, 10, 2 );
请注意,这假设PHP版本为5.4以上