Bhagyashri
asked 3 years ago

//Currently i am using this code but getting empty array
$page_cats_array = get_the_category();
$page_cat = $page_cats_array[0]->name;

$question_args = array(
                        ‘post_type’ => ‘dwqa-question’,
                        ‘posts_per_page’ => 5, //Number of Questions
                        ‘post_status’=>’publish’,
                        ‘taxonomy’ => ‘dwqa-question_category’,
                        // ‘category_name’ => $page_cat,
                        );
                        $qa_attachments = get_posts( $question_args );
                        if ( $qa_attachments ) {
                            foreach ( $qa_attachments as $qws ) { 
                                echo $qws->post_title;
                                echo $qws->ID;
                                $category_detail = get_the_category($qws->ID);//$post->ID
                                foreach ($category_detail as $result) {
                                    echo $result->cat_name; 
                                    echo “<br>”;
                                }                                 
                            }
                        }

1 Answers
DominicStaff
answered 3 years ago

If you want to get the question from the categories, you can refer here:

<?php
                            $options = array(
                                'post_type' => 'dwqa-question',
                                'posts_per_page' => 5,
                                'orderby' => 'date',
                                'order' => 'ASC',
                                'paged' => $paged,
                                'tax_query' => array(
                                    array(
                                        'taxonomy' => 'dwqa-question_category', // Here I have set dummy taxonomy name like "taxonomy_cat" but you must be set current taxonomy name of annoucements post type.
                                        'field' => 'name',
                                        'terms' => 'questions'
                                    )
                                )
                            );
                            $query = new WP_Query( $options );
                            if ( $query->have_posts() ) :
                                while($query->have_posts()) : $query->the_post();
                                    the_title();
                                endwhile; wp_reset_query();
                            endif;
                            ?>

Also, I have checked with your code on our localhost and their code works fine.

Powered by DW Question & Answer Pro