Asri Tadda
asked 5 years ago

Hello,

Any idea how to add RELATED POST below the single question MANUALLY via wordpress theme?
Please share!

2 Answers
Allen
answered 5 years ago

@Asri Tadda : Hello, i think you can create a template name : content-dwqa-relate-question.php

And in there you can use WP_query to query relate question of ( you can query by tag or Category… up to you ) like this :
First : you need to get that single question category:

$categories = wp_get_post_terms(get_the_ID(), 'dwqa-question_category' );
  $cat_id = array();
    foreach ($categories as $category) {
    $cat_id[] = $category->term_id;
}

And then : use Wp_query to get related question:

$post_not_in = array();
$post_not_in[] = get_the_ID();
$question_args = array(
    'post_per_pages' => '5',
    'post_type' => 'dwqa-question',
    'taxonomy' => 'dwqa-question_category',
    'post_status' => 'publish', 
    'post__not_in' => $post_not_in,
    'tax_query' => array(
        array(
        'taxonomy' => 'dwqa-question_category',
        'field' => 'id',
            'terms' => $cat_id,
        ),
    ),
    );
$related_question = new WP_query( $question_args );
if ( $related_question->have_posts() ) {
while ( $related_question->have_posts() ) {
                                                    $related_question->the_post();
                                                    echo '<li>' . get_the_title() . '</li>';
                                                    }

Now you have the list of all question that related by category( this code will exclude the question you are reading ), you need to style them, and then in you question page , you can use
get_template_part( 'content', 'dwqa-relate-question' ) to include the relate question to your single question page.
Glad.

Asri Tadda
answered 5 years ago

Many thanks, nobita!

Powered by DW Question & Answer Pro