Archived topic
Create Archive based on custom field / meta_query
3 replies · Started by Samuel on February 20, 2021
Hi,
I've created a custom field (using CMB2), it's just a checkbox. And now I want to create an archive that show only the posts with the checkbox turned to true.
I was able to create a query that returned only the posts that I want using:
$postsPT = new WP_Query( array(
'post_status' => 'publish',
'post_type' => 'any',
'meta_query' => array(
array(
'key' => 'idioma_checkbox',
'value' => "on",
// 'compare' => 'IN'
)
)
) );
But I can't seem to figure out how the archives work. I'm able to create archives for custom post types, but not based on a custom field.
I duplicated the archive.php template because I want to keep the same design as the posts archive. But can't figure out the best approach.
What template does generate_do_template_part( 'archive' ); uses?
Thanks
Hi there,
Using generate_do_template_part( 'archive' ) will output this template: https://github.com/tomusborne/generatepress/blob/3.0.2/content.php
If you define the custom query in your post template, that template will output the posts from your query.
Hi Tom,
Thanks for the clarification.
I wasn't understanding that it used that template.
I ended up ditching the custom field and was able to achieve what I wanted using a custom taxonomy, which has an archive by default.
Thanks!
Glad you got it sorted! :)