Modifying a query for a archive page in Wordpress
So recently when working I was tasked to modify an archive post that we have. The client wanted their new audio articles that have…
Modifying a query for a archive page in Wordpress
So recently when working I was tasked to modify an archive post that we have. The client wanted their new audio articles that have dictation on them to be integrated into the podcast archive page. A simple enough task sure from an outside prospective as it always is but this essentially means we need to modify our main query and page template to handle the new data coming in.
Setting up
So our work flow is to run our functions from the plugin. So from above we know that we need to modify the query to get multiple post types and do something about the template file being called. So these are the hooks that are going to be called.
These are going to get called in like so;
[embed]
These can of course be called in functions.php by removing ‘$this->loader->’ and ‘, $plugin_public’ but it is good to expose yourself to different workflows as not everyone will code like how you have in the past.
Lets cover ‘podcast_adjust_queries’ first then
[embed]
So after declaring the function we run a few checks on the page.
is_admin() is just good practice
is_post_type_archive( ‘podcast’ ) is the archive post type we are targeting in this case
$query->is_main_query() as we want to modify the main query
get_option ( ‘options_include_audio_articles_in_podcast_archive’ ) is referring to an ACF field I have created which is simply a true/false of whether you want to combine the post types or not. So we check if true with the following if statement.
Now the fun part of modifying our query;
$query->set( ‘post_type’, array( ‘podcast’, ‘articles’ ) );
These are the two post types we are combining in this query.
$query->set( ‘posts_per_page’, 1 );
$query->set( ‘is_podcast_archive’, 1 );
More checks on the query, 1 worked for me but you might want to use -1 or set a limit with a different number
Now the meat of what we are doing with the meta query. My articles that I am looking to combine have some ACF fields that are unique between them. The articles have a true/false state that is that they have an audio version. Furthermore the Podcasts have Hosts associated with them which singles them out as well.
Here is the reference to the WP_Query page on WordPress’s site where there are plenty of resources about how to modify and get the exact data you want.
[embed]WP_Query | Class | WordPress Developer Resources The WordPress Query class.developer.wordpress.org
Get so we have our data, what about that second function we had there?
[embed]
So we are now telling WordPress what template file to call when asked. So in the previous field we are now checking that the query has is_podcast_archive which will mean that it went through our custom filter. This means that we can now select our template to insert and use. And of course if we don’t have that then we don’t need to alter the template being used so $template gets returned untouched in that case.
Now a brief overview of the template file so that you can have some examples to use in your projects;
[embed]
All can be handled through a simple switch statement as they are being displayed differently on the page.
Alright I hope this has given you a brief insight into modifying your WP_Querys. If you have any question feel free to leave a comment.
메타데이터
- post_id
- 4ad00fa69dc9
- slug
- modifying-a-query-for-a-archive-page-in-wordpress-4ad00fa69dc9
- url
- https://medium.com/@calvert_/modifying-a-query-for-a-archive-page-in-wordpress-4ad00fa69dc9
- canonical_url
- https://medium.com/@calvert_/modifying-a-query-for-a-archive-page-in-wordpress-4ad00fa69dc9
- author_url
- https://medium.com/@calvert_
- status
- ok
- fetched_at
- 2026-06-27 08:06:00