自定义进料段塞,以创建自定义进料段塞,例如。
example.tld/billyfeed/
我们可以使用
add_feed()
但我们必须刷新重写规则才能激活它。我们可以这样做,例如访问后端的Permalinks设置页面。
要生成提要,我们可以使用例如。do_feed_rss2()
或do_feed_atom()
.
下面是一个示例:
add_action( \'init\', function()
{
add_feed( \'billyfeed\', function()
{
do_feed_rss2();
});
});
修改自定义提要查询接下来,我们需要修改自定义提要,以便获得作者或类别。
以下是使用posts_clauses
筛选,但不使用字符串替换str_replace()
.
/**
* Custom feed example.tld/billyfeed that queries authors or categories
* Supports pagination
*
* @link http://wordpress.stackexchange.com/a/207876/26350
*/
add_filter( \'posts_clauses\', function( $clauses, \\WP_Query $q )
{
//---------------------------------------
// Only target our custom feed
//---------------------------------------
if( ! $q->is_feed( \'billyfeed\' ) || ! $q->is_main_query() )
return $clauses;
global $wpdb;
//---------------------------------------
// Input - we edit this to our needs
//---------------------------------------
$tax_query = [
[
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => [ \'red\', \'green\', \'blue\' ]
]
];
$author__in = [ 1, 2, 3 ];
//---------------------------------------
// Generate the tax query SQL
//---------------------------------------
$qv = [ \'tax_query\' => $tax_query, \'cat\' => null, \'tag\' => null ];
$q->parse_tax_query( $qv );
$tc = $q->tax_query->get_sql( $wpdb->posts, \'ID\' );
//---------------------------------------
// Generate the author query SQL
//---------------------------------------
$csv = join( \',\', wp_parse_id_list( (array) $author__in ) );
$authors = " {$wpdb->posts}.post_author IN ( $csv ) ";
//---------------------------------------
// Make sure the authors are set and
// the tax query is valid (doesn\'t contain 0 = 1)
//---------------------------------------
if( ! empty( $author__in ) && false === strpos ( $tc[\'where\' ], \' 0 = 1\' ) )
{
// Append to the current join/where parts
$clauses[\'join\'] .= $tc[\'join\'];
$clauses[\'where\'] .= sprintf(
\' AND ( %s OR ( 1=1 %s ) ) \', // The tax query SQL comes prepended with AND
$authors,
$tc[\'where\']
);
}
return $clauses;
}, PHP_INT_MAX, 2 );
我们在其中修改
$tax_query
和
$author__in
满足我们的需求。
这将生成以下主SQL查询:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE
1=1
AND wp_posts.post_type = \'post\'
AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\')
AND ( wp_posts.post_authors IN (1,2,3 ) OR ( 1=1 AND (
wp_term_relationships.term_taxonomy_id IN (1,43,120)
) ) )
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10;
通过这种方法,我们仍然可以对提要分页:
example.tld/billyfeed/?paged=2