@metbril You could probably use a `pre_get_posts` callback (which accepts a `$query` arg) or something and then check for either `is_home()` or `is_front_page()` (not too sure) and `$query->is_main_query()` and then modify `$query` accordingly. Or use a custom query in a `front-page.php` template. 🤓 https://developer.wordpress.org/reference/hooks/pre_get_posts/#basic-examples
@jan That pre_get_posts callback could be in my functions.php, I guess, so no need to change or clone any template file?
@metbril Exactly. First of those basic examples on that page would probably work, just need to change the `post__not_in` bit with `category__not_in` and the correct category ID (or slug, not sure).
@metbril You could (probably) do the same for your “stream” page, by the way, even include CPTs, or remove the post limit (in order to really get all of them instead of paginated results). Instead of `is_front_page()` you would then do `is_page( 'page_ID_or_slug' )` or something.
@jan I'm new to this, so it's a little trial and error. The (sempress) theme has an index.php, no home.php or front-page.php. I've set my home page to a static page and the blog page "/all" to another. The blog page shows the stream just fine (regular WP) but the new home page still shows the regular content (title et al).
@jan I guess the loop only gets that 1 page and won't filter it. In that case I would need a dedicated front-page.php with a custom query?
@metbril A-ha, there’s probably an explicit post (or page) ID in the query parameters already, so you’d have to do more than just exclude a certain category. The page, by definition, will not be in the Uncategorized cat. That’s where WP_Query comes in handy, and the easiest way to set it up is indeed a separate template page. Still, quite sure it could be done, Might help to enable debug logging and do something like `error_log( print_r( $query, true ) )`.
@metbril Was playing around some more and I think it doesn’t work because the “queried object” is set much earlier on (during or just before `parse_request`, I think). There, also `is_page` gets set to `true`, which messes with what you’re trying to do. Just disregarding the original page content and instead looping over a custom WP_Query instance’s results is going to be a lot easier. Seems `pre_get_posts` is really best used for archive pages, feeds, etc.
@jan I'm nearly there. I have replaced the default loop with the results from my custom query ( 'cat'=> -1 ) and that looks promising. I only need to enable pagination. It now only shows the first x posts.
$args = array(
'post_type' => 'post',
'cat' => -1,
$_posts = new WP_Query($args);
@jan This is going to be an interesting journey (or rabbit hole) for me. The pagination is determined by a theme function. That is probably expecting the static page, so no pagination is shown.
@jan Question 😊 ? The navigation function inside my custom query loop accesses 'global $wp_query' Does that always point to the default query or does it use my custom query (in the while loop)?
@metbril I’m not sure. Sounds like that would be the “main” query. Maybe something like this would help? https://stackoverflow.com/questions/53437554/custom-wp-query-with-working-pagination-on-a-page-template
@metbril (Also, make sure you learn about child themes and move your changes there, or they’ll be overwritten with the next theme update!)
@jan I'm already using a child theme. I copied the original index.php to the child theme as front-page.php. 😀
@metbril I may have found a simpler way, after all ... using *custom rewrite rules* and `pre_get_posts`. https://gist.github.com/janboddez/31a44dbf7fa952122c663b5e0606f059
@jan That simpler way at least has the advantage that I a) don't need to point to a static page for the home page and b) don't need to edit any template file. Which means it will continu working when I put this in a site specific plugin (which I will do).
@jan Well, that was easy. I copy/pasted your gist to my mu-plugin file and it worked instantly. I will keep this solution, because it gives me the freedom of switching themes in the future.
@jan This post from stack overflow moved me in the right direction. I have it working now. See this commit: https://github.com/metbril/wordpress-sempress-child/commit/31d3397298eb632e8aebe79ca77bd82c4c18f4c8
@metbril Yeah, your homepage (the static one) will return `true` for `is_front_page()`, on the “blog” page it’s `is_home()` you should use (or not, if it’s already fine).
@metbril The callback runs for every database query, so often even multiple times per page. The `if` statement ensures you modify the query you’re looking for.
The original server operated by the Mastodon gGmbH non-profit