AP_QA_Query_Hooks::posts_results( array $posts, object $instance )

Description #

Add qameta fields to post and prefetch metas and users.

Parameters #

  • $posts
    array (Required) Post array.
  • $instance
    object (Required) QP_Query instance.

Changelog #

VersionDescription
4.1.0Fixed: qameta fields are not appending properly.
3.0.0Introduced.

Source #

File: includes/qaquery-hooks.php

	public static function posts_results( $posts, $instance ) {
		global $question_rendered;

		foreach ( (array) $posts as $k => $p ) {
			if ( in_array( $p->post_type, array( 'question', 'answer' ), true ) ) {
				// Convert object as array to prevent using __isset of WP_Post.
				$p_arr = (array) $p;

				// Check if ptype exists which is a qameta field.
				if ( ! empty( $p_arr['ptype'] ) ) {
					$qameta = ap_qameta_fields();
				} else {
					$qameta = ap_get_qameta( $p->ID );
				}

				foreach ( (array) $qameta as $fields_name => $val ) {
					if ( ! isset( $p_arr[ $fields_name ] ) || empty( $p_arr[ $fields_name ] ) ) {
						$p->$fields_name = $val;
					}
				}

				// Serialize fields and activities.
				$p->activities = maybe_unserialize( $p->activities );
				$p->fields     = maybe_unserialize( $p->fields );

				$p->ap_qameta_wrapped = true;
				$p->votes_net         = $p->votes_up - $p->votes_down;

				// Unset if user cannot read.
				if ( ! ap_user_can_read_post( $p, false, $p->post_type ) ) {
					if ( $instance->is_single() && $instance->is_main_query() ) {
						$posts[ $k ] = self::imaginary_post( $p );
					} else {
						unset( $posts[ $k ] );
					}
				} else {
					$posts[ $k ] = $p;
				}
			}
		}

		if ( isset( $instance->query['ap_question_query'] ) || isset( $instance->query['ap_answers_query'] ) ) {
			$instance->pre_fetch();
		}

		return $posts;
	}

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