1

i have a problem and can't find a solution.

I would like to query in the normal Wordpress archive loop whether a certain meta key is available.

This works with the following code, but then it shows me all posts with the meta key from all categories.

I would like to have all posts with a certain meta key from the current category.

Can someone help me where is the problem?

A heartfelt thank you!

<?php
            $args = array(

                'post_type' => 'post',
                'posts_per_page' => -1,
                'meta_query' => array(
                    array(
                        'relation' => 'OR',
                        array(
                            'key' => 'kategorie_art',
                            'value' => 'Material',
                            'compare' => '='
                        ),

                    ),
                )
            );
            $query = new WP_Query($args);
            while($query -> have_posts()) : $query -> the_post();

            get_template_part( 'template-parts/content', get_post_type() );


            endwhile; wp_reset_query(); ?>

I've also tried querying it with tags but that doesn't work.


<?php if ( have_posts() && is_tag('Material') ) : ?>

            <?php
            /* Start the Loop */
            while ( have_posts() ) :
                the_post();

                /*
                 * Include the Post-Type-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Type name) and that will be used instead.
                 */
                get_template_part( 'template-parts/content', get_post_type() );

            endwhile;

            the_posts_navigation();

        else :

            get_template_part( 'template-parts/content', 'none' );

        endif;
        ?>

Bird
  • 21
  • 3

1 Answers1

1

I've already figured it out myself - this is how it works perfectly! :)

<?php
            $args = array(

                
                'cat' => get_query_var('cat'),
                'post_type' => 'post',
                'posts_per_page' => -1,
                'meta_query' => array(
                    array(
                        'relation' => 'OR',
                        array(
                            'key' => 'kategorie_art',
                            'value' => 'Material',
                            'compare' => '=',
                        ),

                    ),
                )
            );
            
            $query = new WP_Query($args);
            while($query -> have_posts()) : $query -> the_post();

            get_template_part( 'template-parts/content', get_post_type() );


            endwhile; wp_reset_query(); ?>
Bird
  • 21
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 02 '23 at 13:20