$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Query post if has two of the categories|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Query post if has two of the categories

matteradmin6PV0评论

I'm banging my head here. I can't seem to figure out what's wrong with this it works although if a post has more than two categories is doesn't seem to show. What is wrong here?

<div class="base-column">
                        <button class="accordion">How To&nbsp;&nbsp;&nbsp;<img src="/wp-content/uploads/sites/2/2018/11/expand-button.jpg"></button>
                        <div class="panel">
                            <?php // How To
                            $args_1 = array(
                                'post_type'     =>  'post',
                                'category__and' => array(117,123),
                            );

                            // The Query
                            $the_query_1 = new WP_Query( $args_1 ); ?>

                            <?php // The Loop
                            if ( $the_query_1->have_posts() ) {
                                while ( $the_query_1->have_posts() ) {
                                    $the_query_1->the_post(); ?>

                                    <strong>
                                        <a class="title-article" href="<?php echo the_permalink(); ?>">
                                            <?php echo get_the_title(); ?>
                                        </a>
                                    </strong><br>

                                <?php }
                                /* Restore original Post Data */
                                wp_reset_postdata();
                            } else {
                                // no posts found
                            }

                            ?>
                        </div>
                    </div>

I'm banging my head here. I can't seem to figure out what's wrong with this it works although if a post has more than two categories is doesn't seem to show. What is wrong here?

<div class="base-column">
                        <button class="accordion">How To&nbsp;&nbsp;&nbsp;<img src="/wp-content/uploads/sites/2/2018/11/expand-button.jpg"></button>
                        <div class="panel">
                            <?php // How To
                            $args_1 = array(
                                'post_type'     =>  'post',
                                'category__and' => array(117,123),
                            );

                            // The Query
                            $the_query_1 = new WP_Query( $args_1 ); ?>

                            <?php // The Loop
                            if ( $the_query_1->have_posts() ) {
                                while ( $the_query_1->have_posts() ) {
                                    $the_query_1->the_post(); ?>

                                    <strong>
                                        <a class="title-article" href="<?php echo the_permalink(); ?>">
                                            <?php echo get_the_title(); ?>
                                        </a>
                                    </strong><br>

                                <?php }
                                /* Restore original Post Data */
                                wp_reset_postdata();
                            } else {
                                // no posts found
                            }

                            ?>
                        </div>
                    </div>
Share Improve this question edited Nov 16, 2018 at 18:34 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Nov 16, 2018 at 17:57 Mark AnthonyMark Anthony 359 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You need to edit line 7.

'category__and' => array(117,123),

The category__and parameter doesn't use a string for the category IDs. Write the array like this:

    'category__and' => array(
        '117',
        '123'
    )
);

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far