• Resolved Khashayarp

    (@khashayarp)


    Hello,

    I am trying to sort custom post types by recent comments! the purpose is i am writing a faq plugin and i want to show the latest answers with this thing.

    <?php
    global $wp_query;
    
    $wp_query = new WP_Query("post_type=questions&post_status=publish&posts_per_page=10");
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div style="margin:10px;padding:5px;border-bottom:1px dashed #ebebeb;height:auto;">
    <div style="float:right;width:60px;margin-left:10px;" class="questions-page-2">
    <?php echo get_avatar( get_the_author_email(), '48' ); ?>
    </div>
    <div style="float:left;width:450px;" class="questions-page-1">
    <h6 class="toggle"><a href="#" style="opacity: 1;"><?php the_title(); ?></a></h6>
    
    <div class="toggle_content" style="display: block;">
    <div class="block"><?php $content = get_the_excerpt();
    echo substr($content, 0, 120); ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">... show question</a>
    </div>
    
    </div>
    </div>
    <div class="clearboth"></div>
    </div>
    <?php endwhile;
    endif; ?>

    i tried some plugins but they were outdated and didnt work with custom post types

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Khashayarp

    (@khashayarp)

    i tried to figure it out but i didn’t find any paramater for orderby in wp query!
    anybody knows any plugin or code to do that?
    for example:

    $wp_query = new WP_Query( array ( 'post_type' => 'questions', 'posts_per_page' => '10', 'paged' => get_query_var( 'paged' ) , 'orderby' => 'recent_comments' ) );

    is it possible?

    [No bumping, thank you.]

    Thread Starter Khashayarp

    (@khashayarp)

    oh , i figured it out!

    <?php
      $args = array( 'status' => 'approve', 'post_type' => 'questions');
      $comments = get_comments($args);
      $true_comment_count = 0;
    
      foreach($comments as $comment) :
    ?>
    
    <?php $comment_type = get_comment_type(); ?>
    <?php if($comment_type == 'comment') { ?>
    <?php $true_comment_count = $true_comment_count +1; ?>
    <?php $comm_title = get_the_title($comment->comment_post_ID);?>
    <?php $comm_link = get_comment_link($comment->comment_ID);?>
    <?php $comm_comm_temp = get_comment($comment->comment_ID,ARRAY_A);?>
    <?php $comm_content = $comm_comm_temp['comment_content'];?>
    <div style="margin:10px;padding:5px;border-bottom:1px dashed #ebebeb;height:auto;">
    <div style="float:right;width:60px;margin-left:10px;" class="questions-page-2">
    <?php if ( function_exists( 'get_avatar' ) ) {
          echo get_avatar( $comment, 50);
       } ?>
    </div>
    <div style="float:left;width:500px;" class="questions-page-1">
    <h6 class="toggle"><a href="#" style="opacity: 1;"><?php echo $comm_title?></a></h6>
    
    <div class="toggle_content" style="display: block;">
    
    <div class="block"><a href="<?php echo($comm_link)?>" title=""><?php echo $comm_title?></a>
    </div>
    </div>
    </div>
    <div class="clearboth"></div>
    </div>
    <?php } ?>
    <?php if($true_comment_count == 5) {break;} ?>
    <?php endforeach;?>
    
    </div>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sorting custom post types by recent comments!’ is closed to new replies.