ap_get_answer_position_paged( boolean|integer $question_id = false, boolean|integer $answer_id = false )

Description #

Return paged position of answer.

Parameters #

  • $question_id
    boolean | integer (Optional) Question ID. Default value: false
  • $answer_id
    boolean | integer (Optional) Answer ID. Default value: false

Source #

File: includes/answer-loop.php

function ap_get_answer_position_paged( $question_id = false, $answer_id = false ) {
	global $wpdb;

	if ( false === $question_id ) {
		$question_id = get_question_id();
	}

	if ( false === $answer_id ) {
		$answer_id = get_query_var( 'answer_id' );
	}

	$user_id     = get_current_user_id();
	$ap_order_by = ap_get_current_list_filters( 'order_by', 'active' );

	if ( 'voted' === $ap_order_by ) {
		$orderby = 'CASE WHEN IFNULL(qameta.votes_up - qameta.votes_down, 0) >= 0 THEN 1 ELSE 2 END ASC, ABS(qameta.votes_up - qameta.votes_down) DESC';
	} if ( 'oldest' === $ap_order_by ) {
		$orderby = "{$wpdb->posts}.post_date ASC";
	} elseif ( 'newest' === $ap_order_by ) {
		$orderby = "{$wpdb->posts}.post_date DESC";
	} else {
		$orderby = 'qameta.last_updated DESC ';
	}

	$post_status = array( 'publish' );

	// Check if user can read private post.
	if ( ap_user_can_view_private_post() ) {
		$post_status[] = 'private_post';
	}

	// Check if user can read moderate posts.
	if ( ap_user_can_view_moderate_post() ) {
		$post_status[] = 'moderate';
	}

	// Show trash posts to super admin.
	if ( is_super_admin() ) {
		$post_status[] = 'trash';
	}

	$status = "p.post_status IN ('" . implode( "','", $post_status ) . "')";

	$ids = $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM $wpdb->posts p LEFT JOIN $wpdb->ap_qameta qameta ON qameta.post_id = p.ID  WHERE p.post_type = 'answer' AND p.post_parent = %d AND ( $status OR ( p.post_author = %d AND p.post_status IN ('publish', 'private_post', 'trash', 'moderate') ) ) ORDER BY $orderby", $question_id, $user_id ) ); // phpcs:ignore WordPress.DB

	$pos   = (int) array_search( $answer_id, $ids ) + 1; // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
	$paged = ceil( $pos / ap_opt( 'answers_per_page' ) );

	return $paged;
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment