Skip to content

Commit

Permalink
fix: support custom post types in default archive queries
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
dustin-jw committed Nov 7, 2023
1 parent 764f424 commit 481c68a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ The following files will be created based on your inputs:
- `src/php/views/single-<post-type-name>.twig` (optional)
- `src/php/views/archive-<post-type-name>.twig` (optional)

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.

[Custom Post Types documentation](https://wordpress.org/support/article/post-types/#custom-post-types)

### Custom Taxonomy
Expand Down
2 changes: 1 addition & 1 deletion generators/page-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ render_with_password_protection( $timber_post, '${baseFileName}.twig', $context
const twigTemplate = `{% extends "layouts/base.twig" %}
{% block content %}
<div class="obj-width-limiter">
<div>
{{ post.content }}
</div>
{% endblock %}
Expand Down
8 changes: 6 additions & 2 deletions generators/post-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ render_with_password_protection( $timber_post, $templates, $context );
const getSinglePostTemplate = () => `{% extends "layouts/base.twig" %}
{% block content %}
<div class="obj-width-limiter">
<div>
{{ post.content }}
</div>
{% endblock %}
Expand All @@ -85,7 +85,7 @@ Timber\\Timber::render( $templates, $context );
const getArchiveTemplate = () => `{% extends "layouts/base.twig" %}
{% block content %}
<div class="obj-width-limiter">
<div>
{% if posts.found_posts %}
<h1>{{ title }}</h1>
{% for post in posts %}
Expand Down Expand Up @@ -186,6 +186,10 @@ const generatePostType = async () => {
writeFileSync(archiveTemplatePath, archiveTemplate, 'utf-8');
console.log(`Created ${archiveTemplatePath}`);
}

console.log(`
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.
`);
};

generatePostType();
2 changes: 1 addition & 1 deletion generators/taxonomy.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Timber\\Timber::render( $templates, $context );
const getArchiveTemplate = () => `{% extends "layouts/base.twig" %}
{% block content %}
<div class="obj-width-limiter">
<div>
{% if posts.found_posts %}
<h1>{{ title }}</h1>
{% for post in posts %}
Expand Down
31 changes: 29 additions & 2 deletions src/php/inc/setup-queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,35 @@
* @return array
*/
function sb_query_vars_filter( $vars ) {
$vars[] = 'example_var'; // e.g. allow ?example_var="test" in url.
// Allow custom query params in URL.
// e.g. $vars[] = 'example_var'; to allow ?example_var=test in URL.
return $vars;
}
// Uncomment this line if you've added custom query params.
// add_filter( 'query_vars', 'sb_query_vars_filter' );

add_filter( 'query_vars', 'sb_query_vars_filter' );
/**
* Alter default query to fetch posts from custom post types on archive pages.
*
* @param object $query - The incoming query.
* @return object
*/
function enable_custom_posts_in_archives( $query ) {
if ( $query->is_admin() || $query->is_post_type_archive() ) {
return $query;
}

if ( $query->is_archive() || ( $query->is_home() && $query->is_main_query() ) ) {
$query->set(
'post_type',
array(
'post',
// Extend which post types will be queried by default on archive pages.
// e.g. 'recipe', to fetch posts for a custom post type with the slug 'recipe'
)
);
}

return $query;
}
add_filter( 'pre_get_posts', 'enable_custom_posts_in_archives' );
7 changes: 4 additions & 3 deletions src/php/inc/theme-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
* Enqueue scripts and styles.
*/
function sparkpress_theme_scripts() {
$example_data = array( '1', '2', '3' );
wp_enqueue_script( 'sparkpress-theme-script', get_template_directory_uri() . '/js/scripts.js', array(), _S_VERSION, true );
// Defines an `example` JavaScript variable, within the sparkpress-theme-script, equal to $example_data.
wp_localize_script( 'sparkpress-theme-script', 'example', $example_data );

// You can also define global variables that get appended to an enqueued script.
// e.g. wp_localize_script( 'sparkpress-theme-script', 'example', array( '1', '2', '3' ) );
// would define `example` as a global variable with a value of [1, 2, 3]
}
add_action( 'wp_footer', 'sparkpress_theme_scripts' );
3 changes: 1 addition & 2 deletions src/php/inc/theme-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,12 @@ function custom_hide_editor() {
*
* @param mixed $value The current option value.
* @param string $option The name of the option being filtered.
* @param mixed $default The default value for the option.
*
* @return mixed The filtered option value. If the provided option is 'default_ping_status',
* this function will set its value to 'closed' (disabling pingbacks and trackbacks),
* and return the updated value. For other options, it returns the original value.
*/
function custom_disable_pingbacks_trackbacks_option( $value, $option, $default ) {
function custom_disable_pingbacks_trackbacks_option( $value, $option ) {
if ( 'default_ping_status' === $option ) {
$value = 'closed';
}
Expand Down

0 comments on commit 481c68a

Please sign in to comment.