,

How I Made My WordPress Archive Style Different Post Formats

Okay, so a couple days ago I re-built my headless site in WordPress, and in doing so ran into a stumbling block – I couldn’t figure out how to make my feed show different templates based on the post format. I did some digging, and eventually found my way to a pull request that is starting to give some progress toward supporting post formats, but it’s still a long ways away from doing what I’m hoping it will do, considering the request only accounts for querying.

Meanwhile, you have plugins that extend WordPress that tend to move faster than WordPress core. For example the advanced query loop has long-since solved this (and so much more). And…maybe that’s okay? That’s kinda the beauty of WordPress, right?

I’ve been expressing my frustration, and also reaching out to some people online.

Then, Tom Finley and Mike McAlister both jumped into that thread and gave me some ideas, including and one particularly great suggestion – to use another block plugin called block visibility, which makes it possible to conditionally disable certain blocks in your content.

This plugin has some really awesome capabilities, and definitely can solve my problem – all I need to do is set up the query loop with a post template that has two group blocks – one for “aside” post formats, and the other for “standard” post formats.

There’s just one problem – The block visibility plugin doesn’t natively support post formats 😭. Luckily, it does support post meta, however, since it supports post meta, I can filter the post meta field to use post formats to set a meta value dynamically. It’s a little bit of a hack, but it absolutely solves my problem!

By setting the _as_post_format metadata using a filter, I can trick the system into using a meta field that doesn’t actually exist, and is built on the fly with PHP. In other words, I basically transformed the post format (which is technically a taxonomy, fun fact) into a meta field on my site.

Here’s the snippet that I’m using to convert post formats as a post meta, fresh from pluginception, if you’re interested.

<?php
/*
Plugin Name: Post Format As Post Meta
Description: Adds a hook that makes it possible to access the current post 
*/

add_filter('get_post_metadata', function ($value, $post_id, $meta_key, $single) {
	if ('_as_post_format' === $meta_key) {
		// Get the post format for the post
		$post_format = get_post_format($post_id);
		if ($post_format) {
			return $post_format;
		} else {
			return 'standard';
		}
	}

	return $value;
}, 10, 4);

With that snippet in-place, I can add conditionals to my site to display blocks based on the post format. boom baby!

HUGE thanks to Tom and Mike for the nudge in the right direction. This was a big time saver for me!

Comments

2 responses to “How I Made My WordPress Archive Style Different Post Formats”

  1. Alex, this is awesome! I love how quickly you code-switched (LOL) from a strictly programmatic approach to working with the nuances of blocks + code. I trust you’ll be joining Mike McAlister, Brian Coords, and I on lobbying Nick to get Block Visibility merged into core…

    As an aside, would you care to publish this as a Gist or create a drop-in pattern so folks can get right to it?

    1. Yeah, I think that’s a good idea. I’m still processing how to best handle everything I’m doing on this site, because I think there’s a sort of “soft stack” that others might want. I sense a tutorial, post, and other resources coming.

Leave a Reply

Your email address will not be published. Required fields are marked *