Actually “Standard” is not a real post format. It’s just used to indicate in the editor that no specific post format has been specified. As such, it has no archive.
See the post formats documentation for the list of the actual post formats that may be declared: https://developer.wordpress.org/themes/functionality/post-formats/
Thread Starter
Jay
(@topdownjimmy)
Thanks @gappiah — Should it be possible then to navigate to an archive page for which post_format
is not defined? ?post_format=
for instance?
Is there any other way to view an archive of “undefined” post format posts you can think of?
I could create a Page Template that does the query for NOT_EXISTS
on post_format
, but I wish there were a more direct/native way to do this.
I could create a Page Template that does the query for NOT_EXISTS on post_format
That’s how it’s done 🙂 Even if there were a more “direct/native” way to do this, it would simply be a wrapper function that ends up making the very same kind of query. So it might appear to be simpler, but in fact it’s slightly more complicated. Much like how get_posts()
seems simpler, but in fact making a new WP_Query
object is more direct.
Thread Starter
Jay
(@topdownjimmy)
Because the wp:query
block doesn’t natively support the NOT EXISTS
operator, I had to create this custom filter:
// https://wordpress.stackexchange.com/a/422394/2478
function nopostformat_query_loop_block_query_vars( $query, $block ) {
if ( $block->context['query']['noPostFormat'] ) {
$query['tax_query'] = array(
array(
'taxonomy' => 'post_format',
'operator' => 'NOT EXISTS',
),
);
}
return $query;
}
add_filter( 'query_loop_block_query_vars', 'nopostformat_query_loop_block_query_vars', 10, 2 );
Then, in my block template file:
<!-- wp:query { "query": { "perPage": 2, "postType": "post", "order": "desc", "orderBy": "date", "noPostFormat": true } } -->
Thread Starter
Jay
(@topdownjimmy)
I found an even better way to do this.
When you navigate to /type/standard
, WordPress does try to perform a query on post_format = post-format-standard
, but that just doesn’t return anything, because there is no post-format-standard
.
So you can add a pre_get_posts
action to perform this query if WP is “trying” to get standard posts this way:
function standard_posts( $query ) {
if ( $query->get( 'post_format' ) === 'post-format-standard' ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => 'post_format',
'operator' => 'NOT EXISTS'
)
) );
$query->set( 'post_format', NULL );
}
}
add_action( 'pre_get_posts', 'standard_posts' );
The output of the_archive_title
would just be Archives
, so you can fix this in this slightly hacky way:
function standard_posts_archive_title( $title ) {
if ( is_tax() && $title === 'Archives' ) {
return __( 'Standard Posts' );
}
return $title;
}
add_filter( 'get_the_archive_title', 'standard_posts_archive_title', 10, 1 );
-
This reply was modified 1 year, 2 months ago by
Jay.
-
This reply was modified 1 year, 2 months ago by
bcworkz. Reason: 'query' changed to 'get' in action hook
Nice! Thanks for sharing.
Full disclosure: I took the liberty of correcting a minor spelling error in your reply. I hope you don’t mind, we don’t normally mess with people’s post content unless it’s a guideline violation. You had:
“So you can add a pre_query_posts
action to perform…”
while your actual code has the correct pre_get_posts
. TBH, “pre_query_posts” would have actually been more accurate 🙂