我在这里有一个SQL查询:
$data_listg = $wpdb->get_results( "SELECT ID FROM " . $wpdb->prefix . "posts where post_type=\'post\' and SUBSTR(" . $wpdb->prefix . "posts.post_title, 1, 1) LIKE \'%$data\' and post_status=\'publish\' LIMIT 2" );
该查询按第一个单词显示帖子。例如:
如果标题以“A”或“B”开头,则首先显示该帖子。
美式发型奥迪新车
问题是何时更改:
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$args = array(
\'paged\' => $paged,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 2
);
如何将其添加到WP\\U查询中
$args
?
SO网友:Nefro
可以在中使用自定义筛选器$args
(未测试)
add_filter( \'posts_where\', \'find_by_title_filter\', 10, 2 );
function find_by_title_filter( $where, &$wp_query )
{
global $wpdb;
if ( $find_title_arg = $wp_query->get( \'find_title\' ) ) {
$where .= \' AND \' . $wpdb->posts . \'.post_title LIKE \\\'%\' . esc_sql( $wpdb->esc_like( $find_title_arg ) ) . \'\';
}
return $where;
}
$custom_var = \'a\';
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array(
\'paged\' => $paged,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 2,
\'find_title\' => $custom_var
);
SO网友:Ofri
是的解决了我修改了
add_filter( \'posts_where\', \'find_by_title_filter\', 10, 2 );
function find_by_title_filter( $where, &$wp_query )
{
global $wpdb;
if ( $find_title_arg = $wp_query->get( \'find_title\' ) ) {
$where .= " AND SUBSTR(" . $wpdb->posts . ".post_title,1,1) LIKE \'%$find_title_arg%\' ";
}
return $where;
}