Question_Query::__construct( array|string $args = array() )

Description #

Initialize class.

Parameters #

  • $args
    array | string (Optional) Query args. Default value: array()

Changelog #

VersionDescription
unknownunknown
4.1.5Introduced.

Source #

File: includes/qaquery.php

	public function __construct( $args = array() ) {
		if ( is_front_page() ) {
			$paged = ap_sanitize_unslash( 'ap_paged', 'g', 1 );
		} else {
			$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
		}

		if ( isset( $args['post_parent'] ) ) {
			$post_parent = $args['post_parent'];
		} else {
			$post_parent = ( get_query_var( 'parent' ) ) ? get_query_var( 'parent' ) : false;
		}

		if ( ! isset( $args['ap_order_by'] ) ) {
			$args['ap_order_by'] = ap_get_current_list_filters( 'order_by', 'active' );
		}

		$defaults = array(
			'showposts'              => ap_opt( 'question_per_page' ),
			'paged'                  => $paged,
			'ap_query'               => true,
			'ap_order_by'            => 'active',
			'ap_question_query'      => true,
			'post_status'            => array( 'publish' ),
			'ap_current_user_ignore' => false,
		);

		$this->args                = wp_parse_args( $args, $defaults );
		$this->args['ap_order_by'] = sanitize_title( $this->args['ap_order_by'] );

		/**
		 * This was suggested by https://github.com/nash-ye.
		 */
		if ( ! $this->args['ap_current_user_ignore'] ) {
			// Check if user can read private post.
			if ( ap_user_can_view_private_post() ) {
				$this->args['post_status'][] = 'private_post';
			}

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

			// Check if user can read moderate posts.
			if ( ap_user_can_view_future_post() ) {
				$this->args['post_status'][] = 'future';
			}

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

			$this->args['post_status'] = array_unique( $this->args['post_status'] );
		}

		// Show only the unpublished post of author.
		if ( isset( $args['ap_show_unpublished'] ) && true === $this->args['ap_show_unpublished'] ) {
			$this->args['ap_current_user_ignore'] = true;
			$this->args['author']                 = get_current_user_id();
			$this->args['post_status']            = array( 'moderate', 'pending', 'draft', 'trash' );
		}

		if ( $post_parent ) {
			$this->args['post_parent'] = $post_parent;
		}

		if ( '' !== get_query_var( 'ap_s' ) ) {
			$this->args['s'] = ap_sanitize_unslash( 'ap_s', 'query_var' );
		}

		$this->args['post_type'] = 'question';
		parent::__construct( $this->args );
	}

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