Astra_Target_Rules_Fields::get_posts_by_conditions( string $post_type, array $option )

Get posts by conditions


Description


Parameters

$post_type

(string) (Required) Post Type.

$option

(array) (Required) meta option name.


Return

(object|array) Posts.


Source

File: classes/modules/target-rule/class-astra-target-rules-fields.php

		public function get_posts_by_conditions( $post_type, $option ) {

			global $wpdb;
			global $post;

			$post_type = $post_type ? esc_sql( $post_type ) : esc_sql( $post->post_type );

			if ( is_array( self::$current_page_data ) && isset( self::$current_page_data[ $post_type ] ) ) {
				return apply_filters( 'astra_get_display_posts_by_conditions', self::$current_page_data[ $post_type ], $post_type );
			}

			$current_page_type = $this->get_current_page_type();

			self::$current_page_data[ $post_type ] = array();

			$option['current_post_id'] = self::$current_page_data['ID'];
			$meta_header               = self::get_meta_option_post( $post_type, $option );

			/* Meta option is enabled */
			if ( false === $meta_header ) {

				$current_post_type = esc_sql( get_post_type() );
				$current_post_id   = false;
				$q_obj             = get_queried_object();

				$location = isset( $option['location'] ) ? esc_sql( $option['location'] ) : '';

				$query = "SELECT p.ID, pm.meta_value FROM {$wpdb->postmeta} as pm
						   INNER JOIN {$wpdb->posts} as p ON pm.post_id = p.ID
						   WHERE pm.meta_key = '{$location}'
						   AND p.post_type = '{$post_type}'
						   AND p.post_status = 'publish'";

				$orderby = ' ORDER BY p.post_date DESC';

				/* Entire Website */
				$meta_args = "pm.meta_value LIKE '%\"basic-global\"%'";

				switch ( $current_page_type ) {
					case 'is_404':
						$meta_args .= " OR pm.meta_value LIKE '%\"special-404\"%'";
						break;
					case 'is_search':
						$meta_args .= " OR pm.meta_value LIKE '%\"special-search\"%'";
						break;
					case 'is_archive':
					case 'is_tax':
					case 'is_date':
					case 'is_author':
						$meta_args .= " OR pm.meta_value LIKE '%\"basic-archives\"%'";
						$meta_args .= " OR pm.meta_value LIKE '%\"{$current_post_type}|all|archive\"%'";

						if ( 'is_tax' == $current_page_type && ( is_category() || is_tag() || is_tax() ) ) {

							if ( is_object( $q_obj ) ) {
								$meta_args .= " OR pm.meta_value LIKE '%\"{$current_post_type}|all|taxarchive|{$q_obj->taxonomy}\"%'";
								$meta_args .= " OR pm.meta_value LIKE '%\"tax-{$q_obj->term_id}\"%'";
							}
						} elseif ( 'is_date' == $current_page_type ) {
							$meta_args .= " OR pm.meta_value LIKE '%\"special-date\"%'";
						} elseif ( 'is_author' == $current_page_type ) {
							$meta_args .= " OR pm.meta_value LIKE '%\"special-author\"%'";
						}
						break;
					case 'is_home':
						$meta_args .= " OR pm.meta_value LIKE '%\"special-blog\"%'";
						break;
					case 'is_front_page':
						$current_id      = esc_sql( get_the_id() );
						$current_post_id = $current_id;
						$meta_args      .= " OR pm.meta_value LIKE '%\"special-front\"%'";
						$meta_args      .= " OR pm.meta_value LIKE '%\"{$current_post_type}|all\"%'";
						$meta_args      .= " OR pm.meta_value LIKE '%\"post-{$current_id}\"%'";
						break;
					case 'is_singular':
						$current_id = esc_sql( get_the_id() );

						if ( class_exists( 'SitePress' ) ) {
							$default_language = wpml_get_default_language();
							$current_id       = icl_object_id( $current_id, $current_post_type, true, $default_language );
						}

						$current_post_id = $current_id;
						$meta_args      .= " OR pm.meta_value LIKE '%\"basic-singulars\"%'";
						$meta_args      .= " OR pm.meta_value LIKE '%\"{$current_post_type}|all\"%'";
						$meta_args      .= " OR pm.meta_value LIKE '%\"post-{$current_id}\"%'";

						$taxonomies = get_object_taxonomies( $q_obj->post_type );
						$terms      = wp_get_post_terms( $q_obj->ID, $taxonomies );

						foreach ( $terms as $key => $term ) {
							$meta_args .= " OR pm.meta_value LIKE '%\"tax-{$term->term_id}-single-{$term->taxonomy}\"%'";
						}

						break;
					case 'is_woo_shop_page':
						$meta_args .= " OR pm.meta_value LIKE '%\"special-woo-shop\"%'";
						break;
					case '':
						$current_post_id = get_the_id();
						break;
				}

				$meta_args = apply_filters( 'astra_meta_args_post_by_condition', $meta_args, $q_obj, $current_post_id );

				// Ignore the PHPCS warning about constant declaration.
				// @codingStandardsIgnoreStart
				$posts  = $wpdb->get_results( $query . ' AND (' . $meta_args . ')' . $orderby );
				// @codingStandardsIgnoreEnd

				foreach ( $posts as $local_post ) {
					self::$current_page_data[ $post_type ][ $local_post->ID ] = array(
						'id'       => $local_post->ID,
						'location' => maybe_unserialize( $local_post->meta_value ),
					);
				}

				$option['current_post_id'] = $current_post_id;

				$this->remove_exclusion_rule_posts( $post_type, $option );
				$this->remove_user_rule_posts( $post_type, $option );
			}

			return apply_filters( 'astra_get_display_posts_by_conditions', self::$current_page_data[ $post_type ], $post_type );
		}

Changelog

Changelog
Version Description
1.0.0 Introduced.


User Contributed Notes

You must log in before being able to contribute a note or feedback.