Question about #wordpress. I want a regular page with all my post (a stream) and a front page with just a selection (in my case any post NOT in Uncategorized category). Do I need to build a custom `front-page.php` or is there some other nice way to achieve this?

@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. 🤓

@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?

@jan This is the function but the front page still shows the static content, no list of posts.

@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 ) )`.

@jan I'll give a custom template a shot tonight. Thanks for your help so far.

@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.

@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 (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`.

@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 isn't working perfectly 100%. The actual pages are perfect, but the accompanying feeds are not. /feed shows the stream feed and /stream/feed shows the /stream page and doesn't show any feed.

@metbril Oh, would need extra rewrite rules for those.

Sign in to participate in the conversation

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!