-
-
Save janboddez/31a44dbf7fa952122c663b5e0606f059 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Adds a "copy" of your site's "blog" page at `/stream/`. Visit "Settings > | |
* Permalinks" to flush rewrite rules after adding this code. | |
*/ | |
add_action( 'init', function() { | |
add_rewrite_rule( '^stream/feed/?', 'index.php?feed=rss2', 'top' ); | |
add_rewrite_rule( '^stream/page/([0-9]{1,})/?', 'index.php?paged=$matches[1]', 'top' ); | |
add_rewrite_rule( '^stream/?', 'index.php', 'top' ); | |
} ); | |
/** | |
* Excludes a certain category from (only) the (actual) "blog" page. | |
*/ | |
add_action( 'pre_get_posts', function( $query ) { | |
global $wp; | |
if ( $query->is_main_query() | |
&& 0 !== strpos( $wp->request, 'stream' ) | |
&& ( is_home() || ( is_feed() && ! is_comment_feed() && ( empty( $query->query_vars['post_type'] ) || 'post' === $query->query_vars['post_type'] ) ) ) ) { | |
// Main query, not a `stream` URL, and either the "blog" page or a feed (but not a comment feed!) for which the post type is either `post` or not set at all. | |
$query->set( 'category__not_in', array( 1 ) ); | |
} | |
} ); |
The story continues...
The stream page broadcasts only the (now limited) root feed and not the stream feed. For a transparent solution this should be included in the stream <head>
section like so:
<link rel="alternate" type="application/rss+xml" title="Example.com Stream" href="https://example.com/stream/feed/">
LOL: this also applies for any other "free" feed, like the ATOM, JSON or MF2 feeds.
I'm slowly getting the idea that this will become a never ending story.
I've added some extra rewrites for all (known to me) possible feeds.
Smart! On one of my blogs, I do it the other way around. All "standard" feeds except RSS2 (for which I use a custom template, that's why) lead to a 404 error. Then I only have to worry about that one anymore. :-)
As far as I've been able to see, the pre_get_posts gives me all the feeds out of the box with the filtered posts.
Hmm, not sure if I understand it correctly, but this bit should actually prevent that:
0 !== strpos( $wp->request, 'stream' )
You might wanna think of an alternative, then. 0 !== strpos( $_SERVER['REQUEST_URI'], '/stream' )
or something.
To work around this, couldn't you just exclude pages only?
Hm, that would also target, say, category or CPT feeds, and so on. You really want to target only the main feed, and that takes some knowledge on what is really included there. Like, if in another must-use plugin you include a CPT or something, you may want to explicitly check for array( 'post', 'my_cpt_slug' ) === $query->query_vars['post_type']
instead. Only way, I think, to guarantee you're targeting the main feed only.
Come to think of it, my version probably also targets category feeds. Better to explicitly check the URL, using, e.g., 'feed' === $wp->request
. (Untested!)
The best way to go about this is probably "debug log" the $wp
and $query
objects and see what parameters are actually present for the feeds you want to target.
The stream page broadcasts only the (now limited) root feed and not the stream feed.
This is normal. WordPress doesn't know about the feed. Same reason it wasn't "auto-generated." Using rewrite rules like this is much more low-level than, e.g., custom post types, which give you a ton of functionality "for free." All we're doing is defining new "routes" and linking them to a "controller method." Nothing else ("magically") happens.
Look into the wp_head
action, which will allow you to add markup to your site's <head>
.
To work around this, couldn't you just exclude pages only? So replace
with