Skip to content

Commit 481c68a

Browse files
committed
fix: support custom post types in default archive queries
- turn example code into a comment - remove redundant class in generated page templates - update instructions for creating post types - comment out unnecessary add_filter call
1 parent 764f424 commit 481c68a

File tree

7 files changed

+44
-11
lines changed

7 files changed

+44
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ The following files will be created based on your inputs:
350350
- `src/php/views/single-<post-type-name>.twig` (optional)
351351
- `src/php/views/archive-<post-type-name>.twig` (optional)
352352

353+
To allow for posts from your new custom post type to be shown on archive pages, you will also need to update `src/php/inc/setup-queries.php` to look for posts and your custom post types by default.
354+
353355
[Custom Post Types documentation](https://wordpress.org/support/article/post-types/#custom-post-types)
354356

355357
### Custom Taxonomy

generators/page-template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ render_with_password_protection( $timber_post, '${baseFileName}.twig', $context
1818
const twigTemplate = `{% extends "layouts/base.twig" %}
1919
2020
{% block content %}
21-
<div class="obj-width-limiter">
21+
<div>
2222
{{ post.content }}
2323
</div>
2424
{% endblock %}

generators/post-type.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ render_with_password_protection( $timber_post, $templates, $context );
6363
const getSinglePostTemplate = () => `{% extends "layouts/base.twig" %}
6464
6565
{% block content %}
66-
<div class="obj-width-limiter">
66+
<div>
6767
{{ post.content }}
6868
</div>
6969
{% endblock %}
@@ -85,7 +85,7 @@ Timber\\Timber::render( $templates, $context );
8585
const getArchiveTemplate = () => `{% extends "layouts/base.twig" %}
8686
8787
{% block content %}
88-
<div class="obj-width-limiter">
88+
<div>
8989
{% if posts.found_posts %}
9090
<h1>{{ title }}</h1>
9191
{% for post in posts %}
@@ -186,6 +186,10 @@ const generatePostType = async () => {
186186
writeFileSync(archiveTemplatePath, archiveTemplate, 'utf-8');
187187
console.log(`Created ${archiveTemplatePath}`);
188188
}
189+
190+
console.log(`
191+
Note: to get posts from your new custom post type to show up on archive pages, you will need to update enable_custom_posts_in_archives in ./src/php/inc/setup-queries.php.
192+
`);
189193
};
190194

191195
generatePostType();

generators/taxonomy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Timber\\Timber::render( $templates, $context );
5252
const getArchiveTemplate = () => `{% extends "layouts/base.twig" %}
5353
5454
{% block content %}
55-
<div class="obj-width-limiter">
55+
<div>
5656
{% if posts.found_posts %}
5757
<h1>{{ title }}</h1>
5858
{% for post in posts %}

src/php/inc/setup-queries.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,35 @@
1010
* @return array
1111
*/
1212
function sb_query_vars_filter( $vars ) {
13-
$vars[] = 'example_var'; // e.g. allow ?example_var="test" in url.
13+
// Allow custom query params in URL.
14+
// e.g. $vars[] = 'example_var'; to allow ?example_var=test in URL.
1415
return $vars;
1516
}
17+
// Uncomment this line if you've added custom query params.
18+
// add_filter( 'query_vars', 'sb_query_vars_filter' );
1619

17-
add_filter( 'query_vars', 'sb_query_vars_filter' );
20+
/**
21+
* Alter default query to fetch posts from custom post types on archive pages.
22+
*
23+
* @param object $query - The incoming query.
24+
* @return object
25+
*/
26+
function enable_custom_posts_in_archives( $query ) {
27+
if ( $query->is_admin() || $query->is_post_type_archive() ) {
28+
return $query;
29+
}
30+
31+
if ( $query->is_archive() || ( $query->is_home() && $query->is_main_query() ) ) {
32+
$query->set(
33+
'post_type',
34+
array(
35+
'post',
36+
// Extend which post types will be queried by default on archive pages.
37+
// e.g. 'recipe', to fetch posts for a custom post type with the slug 'recipe'
38+
)
39+
);
40+
}
41+
42+
return $query;
43+
}
44+
add_filter( 'pre_get_posts', 'enable_custom_posts_in_archives' );

src/php/inc/theme-scripts.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
* Enqueue scripts and styles.
1010
*/
1111
function sparkpress_theme_scripts() {
12-
$example_data = array( '1', '2', '3' );
1312
wp_enqueue_script( 'sparkpress-theme-script', get_template_directory_uri() . '/js/scripts.js', array(), _S_VERSION, true );
14-
// Defines an `example` JavaScript variable, within the sparkpress-theme-script, equal to $example_data.
15-
wp_localize_script( 'sparkpress-theme-script', 'example', $example_data );
13+
14+
// You can also define global variables that get appended to an enqueued script.
15+
// e.g. wp_localize_script( 'sparkpress-theme-script', 'example', array( '1', '2', '3' ) );
16+
// would define `example` as a global variable with a value of [1, 2, 3]
1617
}
1718
add_action( 'wp_footer', 'sparkpress_theme_scripts' );

src/php/inc/theme-setup.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,12 @@ function custom_hide_editor() {
101101
*
102102
* @param mixed $value The current option value.
103103
* @param string $option The name of the option being filtered.
104-
* @param mixed $default The default value for the option.
105104
*
106105
* @return mixed The filtered option value. If the provided option is 'default_ping_status',
107106
* this function will set its value to 'closed' (disabling pingbacks and trackbacks),
108107
* and return the updated value. For other options, it returns the original value.
109108
*/
110-
function custom_disable_pingbacks_trackbacks_option( $value, $option, $default ) {
109+
function custom_disable_pingbacks_trackbacks_option( $value, $option ) {
111110
if ( 'default_ping_status' === $option ) {
112111
$value = 'closed';
113112
}

0 commit comments

Comments
 (0)