[Resolved] Create Archive based on custom field / meta_query

Home Forums Support [Resolved] Create Archive based on custom field / meta_query

Home Forums Support Create Archive based on custom field / meta_query

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1665879
    Samuel

    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

    #1666814
    Tom
    Lead Developer
    Lead Developer

    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.

    #1670791
    Samuel

    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!

    #1671028
    Tom
    Lead Developer
    Lead Developer
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.