Roots Discourse

Where should I put pre_get_posts in Sage 10?

Just trying to learn a bit more about the directory structure in Sage 10.

There are 4 php files in Sage 10 app directory:

  1. admin
  2. filters
  3. helpers
  4. setup

I understand that admin is for admin stuff, filters if for… filters, helpers not sure, and setup is theme init stuff.

Now, lets say I’m using ACF and want to add a function to do something similar to this:

Where does it make sense to put an add_action('pre_get_posts' function ($query)?

I mean logically, where does it ‘fit’. Right now I have it in the setup.php, but I think that might not be the most logical place for it. I’m trying to understand what might make sense to go where in order to keep things readable, coherent, and organized.

Not sure how others would approach this, but you can add a new file to the app directory and then edit the bootstrap/app.php to reference it. That would help keep things organized.

Add app/meaningfulname.php and include namespace App; at the top

Edit bootstrap/app.php

collect(['helpers', 'setup', 'filters', 'admin', 'meaningfulname'])
2 Likes