/** * @param string $type * @param array $conditions * @return array */ public function find($type = 'all', $conditions = array()) { $conditions = array_merge(array('conditions' => array(), 'sort' => 'descending', 'limit' => null), $conditions); extract($conditions, EXTR_OVERWRITE); $conditions['limit'] = $limit; $conditions = $this->_parseQueryConditions($conditions); // debug($conditions); $WP_Query = new WP_Query($conditions); $posts = $WP_Query->get_posts(); wp_reset_query(); $result = array(); switch ($type) { case 'first': $result = reset($posts); break; case 'list': foreach ($posts as $post) { $result[$post->ID] = $post->post_title; } break; default: $result = $posts; } return $result; }
function get_images($page, $per_page, $options = array()) { $result = array(); if (isset($options['images']) && $options['images']) { $ids = array_unique(array_map('intval', preg_split("/\\s*,\\s*/", $options['images']))); $query = new WP_Query(array('post__in' => array($ids), 'ignore_sticky_posts' => true, 'post_type' => 'any', 'post_status' => 'any', 'paged' => $page, 'posts_per_page' => $per_page)); $posts = $query->get_posts(); } else { if ($this->source['source'] == 'featured') { $posts = $this->get_posts($page, $per_page); } else { $posts = $this->get_attachments($page, $per_page); } } foreach ($posts as $post) { if ($post) { $image = $this->build_image($post, $options); if ($image) { $result[] = $image; $image->attachment_id = $post->ID; } } } return $result; }
public function getPosts() { /** By default wp will return all posts... override functionality so it returns no posts */ if (isset($this->args['post__in']) && empty($this->args['post__in'])) { $this->posts = array(); $this->pagination = array(); return $this->posts; } if ($this->args == $this->argsHistory) { return $this->posts; } // push args to history $this->argsHistory = $this->args; // query posts $wpQuery = new WP_Query($this->args); $this->posts = $this->generateProxies($wpQuery->get_posts()); global $paged; global $wp_rewrite; $this->pagination = $this->calculatePagination(array('mid_size' => '1'), $wpQuery, $wp_rewrite, $paged); $this->pagination['found_posts'] = $wpQuery->found_posts; $this->pagination['max_num_pages'] = $wpQuery->max_num_pages; $this->pagination['post_count'] = $wpQuery->post_count; $this->pagination['posts_per_page'] = isset($wpQuery->query['posts_per_page']) ? $wpQuery->query['posts_per_page'] : 10; $this->pagination['page'] = isset($wpQuery->query['paged']) ? $wpQuery->query['paged'] : 1; $this->pagination['lower_index'] = ($this->pagination['page'] - 1) * $this->pagination['post_count'] + 1; $this->pagination['upper_index'] = $this->pagination['page'] * $this->pagination['post_count']; return $this->posts; }
/** * Get posts. * * @param stdClass $settings * * @return array */ protected function get_posts($settings) { // By default we add posts per page key with the value -1 (all). if (!isset($settings->query['posts_per_page'])) { $settings->query['posts_per_page'] = -1; } // Prepare arguments for WP_Query. $args = array_merge($settings->query, ['post_type' => papi_to_array($settings->post_type), 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false]); $query = new WP_Query($args); $posts = $query->get_posts(); // Keep only objects. $posts = papi_get_only_objects($posts); $results = []; // Set labels. foreach ($posts as $post) { $obj = get_post_type_object($post->post_type); if (empty($obj)) { continue; } if (!isset($results[$obj->labels->menu_name])) { $results[$obj->labels->menu_name] = []; } $results[$obj->labels->menu_name][] = $post; } return $results; }
function get_info_item($id) { $id = base64_decode($id); $args = array('post_type' => "number", 'p' => $id); $query = new WP_Query($args); return $query->get_posts(); }
function tfuse_members($atts, $content = null) { extract(shortcode_atts(array('post' => ''), $atts)); $output = ''; $post = explode(',', $post); $query = new WP_Query(array('posts_per_page' => -1, 'post_type' => 'member', 'post__in' => $post)); $posts = $query->get_posts(); if (!empty($posts)) { $output .= '<div class="our-team">'; foreach ($posts as $post) { $job = tfuse_page_options('job', '', $post->ID); $image = wp_get_attachment_url(get_post_thumbnail_id($post->ID, 'post-thumbnails')); if (!empty($image)) { $image = TF_GET_IMAGE::get_src_link($image, 270, 270); } $output .= '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6"> <div class="image-our-team"> <img src="' . $image . '" alt="' . $post->post_title . '"> </div> <h3>' . $post->post_title . '</h3> <span class="function">' . $job . '</span> </div>'; } $output .= '</div>'; } return $output; }
function tfuse_latest_reviews($atts, $content = null) { extract(shortcode_atts(array('title' => '', 'items' => ''), $atts)); $query = new WP_Query(array('post_type' => 'review', 'orderby' => 'post_date', 'order ' => 'DESC', 'posts_per_page' => $items)); $posts = $query->get_posts(); $return_html = $img = ''; if (!empty($posts)) { $return_html .= '<div class="widget widget_recent_entries"> <h3 class="widget-title">Latest Reviews</h3> <ul class="side-postlist">'; foreach ($posts as $post) { $post_image_src = wp_get_attachment_url(get_post_thumbnail_id($post->ID, 'post-thumbnails')); if (!empty($post_image_src)) { $get_image = new TF_GET_IMAGE(); $img = $get_image->properties(array('class' => '', 'alt' => get_the_title($post->ID)))->width(62)->height(62)->src($post_image_src)->resize(true)->get_img(); } $return_html .= '<li>'; if (!empty($img)) { $return_html .= '<a href="' . get_permalink($post->ID) . '" class="post-thumbnail"> ' . $img . ' </a>'; } $return_html .= '<a href="' . get_permalink($post->ID) . '" class="post-title">' . get_the_title($post->ID) . '</a> <span class="comments-link"><a href="' . get_permalink($post->ID) . '#comments">' . tfuse_get_comments(true, $post->ID) . '</a></span> </li>'; } $return_html .= '</ul> </div>'; } return $return_html; }
function test_put() { RemoteSyncPlugin::instance()->install(); $api = RemoteSyncPlugin::instance()->getApi(); $data = array("post_name" => "some-slug", "post_type" => "page", "post_content" => "hello", "post_title" => "title", "post_excerpt" => "ex", "post_status" => "published", "post_parent" => NULL, "menu_order" => 0); try { $api->put(array("baseRevision" => "123", "slug" => "some-slug", "type" => "post", "data" => json_encode($data))); } catch (Exception $e) { $exceptionMessage = $e->getMessage(); } $this->assertEquals($exceptionMessage, "Doesn't exist locally"); $data = array("post_name" => "some-slug", "post_type" => "page", "post_content" => "hello", "post_title" => "title", "post_excerpt" => "ex", "post_status" => "published", "post_parent" => "", "menu_order" => 0, "meta" => array()); $api->add(array("slug" => "some-slug", "type" => "post", "data" => json_encode($data))); $resData = $api->get(array("type" => "post", "slug" => "some-slug")); $this->assertEquals("98caf22f76c2d40eed50cf642db03e8b", $resData["revision"]); $data["post_content"] = "some new content"; try { $api->put(array("slug" => "some-slug", "type" => "post", "baseRevision" => "wrong", "data" => json_encode($data))); } catch (Exception $e) { $exceptionMessage = $e->getMessage(); } $this->assertEquals($exceptionMessage, "Wrong base revision, please pull."); $api->put(array("slug" => "some-slug", "type" => "post", "baseRevision" => "98caf22f76c2d40eed50cf642db03e8b", "data" => json_encode($data))); $q = new WP_Query(array("post_type" => "any", "post_status" => "any", "post_name" => "some-slug")); $posts = $q->get_posts(); $this->assertEquals(1, sizeof($posts)); $this->assertEquals($posts[0]->post_content, "some new content"); }
function tfuse_faq($atts, $content = null) { extract(shortcode_atts(array('post' => ''), $atts)); $output = ''; $post = explode(',', $post); $query = new WP_Query(array('posts_per_page' => -1, 'post_type' => 'faq', 'post__in' => $post)); $posts = $query->get_posts(); if (!empty($posts)) { foreach ($posts as $post) { $btn_title = tfuse_page_options('btn_title', '', $post->ID); $output .= '<div class="faq-article clearfix"> <div class="col-md-8 col-sm-7 col-xs-7 text"> <h2>Q: ' . $post->post_title . '</h2> <p>' . $post->post_content . '</p> </div>'; if (!empty($btn_title)) { $output .= '<div class="col-md-3 col-md-offset-1 col-sm-5 col-xs-5 faq-button"> <a href="' . tfuse_page_options('btn_link', '', $post->ID) . '" class="btn btn-yellow btn-donate-faq"><span>' . $btn_title . '</span></a> </div>'; } $output .= '</div>'; } // End IF Statement } return $output; }
function wpSinglePostBySlug($slug, $type) { $query = new \WP_Query(array('pagename' => $slug, 'post_type' => $type)); //$p = get_post($id); $post = $query->get_posts(); return $post; }
/** * Get posts via GET. * * Posts with empty title will be ignored. * * GET /papi-ajax/?action=get_posts */ public function get_posts() { $args = papi_get_qs('query') ?: []; $args = is_array($args) ? $args : []; $fields = papi_get_qs('fields') ?: []; $fields = is_array($fields) ? $fields : []; $query = new WP_Query(array_merge(['post_type' => ['post'], 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false], $args)); $posts = $query->get_posts(); $posts = array_filter($posts, function ($post) { return !empty($post->post_title); }); // @codeCoverageIgnoreStart usort($posts, function ($a, $b) { return strcmp(strtolower($a->post_title), strtolower($b->post_title)); }); // @codeCoverageIgnoreEnd if (!empty($fields)) { foreach ($posts as $index => $post) { $item = []; foreach ($fields as $field) { $item[$field] = $post->{$field}; } $posts[$index] = $item; } } wp_send_json($posts); }
function test_ajax() { header("content-type: applications/json"); $query = new WP_Query(array('numberposts' => 10, 'order' => 'DESC', 'orderby' => 'title', 'post_type' => 'post', 'thumbnail' => 'post')); echo json_encode($query->get_posts()); die; }
function tpp_posts_widget() { $tpp_posts_query = new WP_Query(); $tpp_posts_query->get_posts(); ?> <h3>Posts on this page:</h3> <?php if ($tpp_posts_query->have_posts()) { while ($tpp_posts_query->have_posts()) { $tpp_posts_query->the_post(); ?> <a href="<?php echo the_permalink(); ?> " title="<?php echo the_title(); ?> "><?php echo the_title(); ?> </a> (<?php echo comments_number(); ?> ) <?php } } ?> <?php }
public function get_banners($custom_args = array()) { $args = array('posts_per_page' => -1, 'post_type' => 'banners', 'post_status' => 'publish'); $query = new WP_Query(array_merge($args, $custom_args)); return $query->get_posts(); //return get_posts( wp_parse_args( $custom_args, $args ) ); }
function jigoshop_product_list($attributes) { $options = Jigoshop_Base::get_options(); $attributes = shortcode_atts(array('number' => $options->get('jigoshop_catalog_per_page'), 'order_by' => 'date', 'order' => 'desc', 'orientation' => 'rows', 'taxonomy' => 'product_cat', 'terms' => '', 'thumbnails' => 'show', 'sku' => 'hide'), $attributes); $args = array('post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => $attributes['number'], 'orderby' => $attributes['order_by'], 'order' => $attributes['order'], 'meta_query' => array(array('key' => 'visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN'))); if (!empty($attributes['taxonomy']) && !empty($attributes['terms'])) { $args['tax_query'] = array(array('taxonomy' => $attributes['taxonomy'], 'terms' => $attributes['terms'], 'field' => 'slug')); } $query = new WP_Query($args); remove_action('jigoshop_before_shop_loop_item_title', 'jigoshop_template_loop_product_thumbnail', 10); if ($attributes['thumbnails'] === 'show') { add_action('jigoshop_before_shop_loop_item', 'jigoshop_product_thumbnail', 10, 2); } if ($attributes['sku'] === 'show') { add_action('jigoshop_after_shop_loop_item_title', 'jigoshop_product_sku', 9, 2); } $result = jigoshop_render_result('shortcode/product_list', array('orientation' => $attributes['orientation'], 'products' => $query->get_posts(), 'has_thumbnails' => $attributes['thumbnails'] === 'show')); if ($attributes['sku'] === 'show') { remove_action('jigoshop_after_shop_loop_item_title', 'jigoshop_product_sku', 9); } if ($attributes['thumbnails'] === 'show') { remove_action('jigoshop_before_shop_loop_item', 'jigoshop_product_thumbnail', 10); } add_action('jigoshop_before_shop_loop_item_title', 'jigoshop_template_loop_product_thumbnail', 10, 2); return $result; }
function hm_wcst_calculate() { $wp_query = new WP_Query(array('post_type' => 'product', 'fields' => 'ids', 'nopaging' => true)); $productIds = $wp_query->get_posts(); $periodDays = get_option('hm_wcst_period_days', 7); $periodEnd = current_time('timestamp'); $periodStart = $periodEnd - $periodDays * 86400; update_option('hm_wcst_current_period_start', $periodStart); update_option('hm_wcst_current_period_end', $periodEnd); $currentSales = hm_wcst_get_product_sales($productIds, 'hm_wcst_sales_current', $periodStart, $periodEnd); $periodEnd = $periodStart - 1; $periodStart = $periodEnd - $periodDays * 86400; update_option('hm_wcst_previous_period_start', $periodStart); update_option('hm_wcst_previous_period_end', $periodEnd); $previousSales = hm_wcst_get_product_sales($productIds, 'hm_wcst_sales_previous', $periodStart, $periodEnd); foreach ($productIds as $productId) { if ($currentSales[$productId] > $previousSales[$productId]) { if ($previousSales[$productId] > 0) { update_post_meta($productId, 'hm_wcst_trend', ($currentSales[$productId] - $previousSales[$productId]) / $previousSales[$productId] * 100); } else { update_post_meta($productId, 'hm_wcst_trend', 99999999); } } else { if ($currentSales[$productId] < $previousSales[$productId]) { update_post_meta($productId, 'hm_wcst_trend', ($previousSales[$productId] - $currentSales[$productId]) / $previousSales[$productId] * -100); } else { update_post_meta($productId, 'hm_wcst_trend', 0); } } } update_option('hm_wcst_last_update', current_time('timestamp')); }
/** * Generate a proper format of triggers/alerts to be used/cached * * @param bool $force_refresh Ignore cached version * * @return array|mixed|void */ public function rules($force_refresh = false) { if (defined('WP_DEBUG') && WP_DEBUG) { $force_refresh = true; } // Check if we have a valid cache if (!$force_refresh && false !== ($rules = get_transient(self::CACHE_KEY))) { return $rules; } // Get rules $args = array('post_type' => WP_Stream_Notifications_Post_Type::POSTTYPE, 'post_status' => 'publish', 'posts_per_page' => -1); $query = new WP_Query($args); $rules = $query->get_posts(); /** * Allow developers to add/modify rules * * @param array $rules Rules for the current blog * @param array $args Query args used * * @return array */ $rules = apply_filters('wp_stream_notifications_rules', $rules, $args); $rules = $this->format($rules); // Cache the new rules set_transient(self::CACHE_KEY, $rules); return $rules; }
public function cpt_query() { //Get all Posts from Posts type name global $json_api; global $post; global $wp_post_types; if (isset($_GET["cpt_name"])) { $posts_name_cpt_api = $_GET["cpt_name"]; if (isset($_GET["cpt_status"])) { $posts_status_cpt_api = $_GET["cpt_status"]; } else { $posts_status_cpt_api = "publish"; } $query = new WP_Query(array('post_type' => $posts_name_cpt_api, 'post_status' => $posts_status_cpt_api, 'meta_query' => array(array('compare' => '!=', 'type' => 'UNSIGNED')))); $results = $query->get_posts(); if ($results) { return array('description' => 'post type:' . $posts_name_cpt_api, 'post' => $results); // Restore original post data. wp_reset_postdata(); } else { $json_api->error("Please insert valid Post Type Name"); // Restore original post data. wp_reset_postdata(); } } else { $json_api->error("Please format your api like '?cpt_name=your_post_type' and optionally you can select the status with '&cpt_status=draft'"); } }
/** * @param Request $request * @return Response */ public function all(Request $request) { $post_type = $_GET['type'] ?: "post"; $query = new \WP_Query(["post_type" => $post_type]); $posts = new Collection($query->get_posts(), new PostTransformer(), "posts"); return new JsonResponse\Ok($this->fractal->createData($posts)->toArray()); }
/** * Modify arguments for WP_Query on listing pages when searching for a string in Views, Content Templates * or WordPress Archives. * * This function will search for given string in titles and descriptions. It returns a modified array of arguments * for the "final" query on a listing page with the "post__in" argument containing array View/CT/WPA IDs where matching * string was found. * * Post meta key containing description will be determined from 'post_type' argument in $wpv_args. * * @param string $s Searched string (will be sanitized). * @param array $wpv_args Arguments for the listing WP_Query. They must contain the 'post_type' key with value * either 'view' or 'view-template'. * * @return array Modified $wpv_args for the listing query. * * @since 1.7 */ function wpv_modify_wpquery_for_search( $s, $wpv_args ) { $s_param = urldecode( sanitize_text_field( $s ) ); $results = array(); $search_args = $wpv_args; $search_args['posts_per_page'] = '-1'; // return all posts $search_args['fields'] = 'ids'; // return only post IDs // First, search in post titles $titles_search_args = $search_args; $titles_search_args['s'] = $s_param; $query_titles = new WP_Query( $titles_search_args ); $title_results = $query_titles->get_posts(); if( !is_array( $title_results ) ) { $title_results = array(); } // Now search in description. // Determine description meta_key based on post type. $description_key = ''; switch( $wpv_args['post_type'] ) { // This covers both Views and WPAs. case 'view': $description_key = '_wpv_description'; break; // Content templates. case 'view-template': $description_key = '_wpv-content-template-decription'; break; } $description_search_args = $search_args; $description_search_args['meta_query'] = array( array( 'key' => $description_key, 'value' => $s_param, 'compare' => 'LIKE' ) ); $query_description = new WP_Query( $description_search_args ); $description_results = $query_description->get_posts(); if( !is_array( $description_results ) ) { $description_results = array(); } // Merge results from both queries. $results = array_unique( array_merge( $title_results, $description_results ) ); // Modify arguments for the final query if ( count( $results ) == 0 ) { $wpv_args['post__in'] = array( '0' ); } else { $wpv_args['post__in'] = $results; } return $wpv_args; }
/** * Generate array with: recent/popular/most commented posts * @param string $sort Sort type (recent/popular/most commented) * @param integer $items Number of items to be extracted * @param boolean $image_post Extract or no post image * @param integer $image_width Set width of post image * @param integer $image_height Set height of post image * @param string $image_class Set class of post image * @param boolean $date_post Extract or no post date * @param string $date_format Set date format * @param string $post_type Set post type * @param string $category Set category from where posts would be extracted * @since Medica 2.0 */ function tfuse_shortcode_posts($args = null) { $defaults = array('sort' => 'recent', 'items' => 5, 'image_post' => true, 'image_width' => 54, 'image_height' => 54, 'image_class' => 'thumbnail', 'date_post' => true, 'date_format' => 'F jS, Y', 'post_type' => 'post', 'category' => '', 'specialites' => ''); extract(wp_parse_args($args, $defaults)); global $post; $tf_cat_ID = !empty($category) && empty($specialites) ? get_cat_ID($category) : ''; $specialites = empty($category) && !empty($specialites) ? $specialites : ''; if ($sort == 'recent') { $query = new WP_Query(array('post_type' => $post_type, 'orderby' => 'post_date', 'order ' => 'DESC', 'cat' => $tf_cat_ID, 'Specialites' => $specialites, 'posts_per_page' => $items)); $posts = $query->get_posts(); } elseif ($sort == 'popular') { $query = new WP_Query(array('post_type' => $post_type, 'orderby' => 'meta_value', 'meta_key' => TF_THEME_PREFIX . '_post_viewed', 'order ' => 'DESC', 'cat' => $tf_cat_ID, 'posts_per_page' => $items)); $posts = $query->get_posts(); } elseif ($sort == 'commented') { $query = new WP_Query(array('post_type' => $post_type, 'orderby' => 'comment_count', 'order ' => 'DESC', 'cat' => $tf_cat_ID, 'posts_per_page' => $items)); $posts = $query->get_posts(); } else { return false; } $tf_post_option = array(); $count = 0; if (!empty($posts)) { foreach ($posts as $post_elm) { setup_postdata($post_elm); $img = ''; if ($image_post == true) { $post_image_src = tfuse_page_options('thumbnail_image', tfuse_page_options('single_image', null, $post_elm->ID), $post_elm->ID); if (!empty($post_image_src)) { $get_image = new TF_GET_IMAGE(); $img = $get_image->properties(array('class' => $image_class, 'alt' => get_the_title($post_elm->ID)))->width($image_width)->height($image_height)->src($post_image_src)->resize(true)->get_img(); } } if (!empty($img)) { $tf_post_option[$count]['post_img'] = $img; } else { $tf_post_option[$count]['post_img'] = ''; } if ($date_post) { $time_format = apply_filters('tfuse_widget_time_format', $date_format); $tf_post_option[$count]['post_date_post'] = get_the_time($time_format, $post_elm->ID); } else { $tf_post_option[$count]['post_date_post'] = ''; } $tf_post_option[$count]['post_class'] = is_singular() && $post->ID == $post_elm->ID ? 'current-menu-item post_' . $sort : 'post_' . $sort; $tf_post_option[$count]['post_title'] = get_the_title($post_elm->ID); $tf_post_option[$count]['post_link'] = get_permalink($post_elm->ID); $tf_post_option[$count]['post_author_link'] = get_author_posts_url(get_the_author_meta('ID')); $tf_post_option[$count]['post_author_name'] = get_the_author(); $tf_post_option[$count]['post_comnt_numb'] = get_comments_number($post_elm->ID); $tf_post_option[$count]['post_comnt_numb_link'] = tfuse_get_comments(true, $post_elm->ID); $tf_post_option[$count]['post_loveit_number'] = get_post_meta($post_elm->ID, TF_THEME_PREFIX . '_post_viewed', true); $tf_post_option[$count]['post_excerpt'] = apply_filters('get_the_excerpt', $post_elm->post_excerpt); $count++; } wp_reset_postdata(); } return $tf_post_option; }
function widget($args, $instance) { global $post; $query_args = array('posts_per_page' => 5, 'meta_key' => 'luot_xem', 'order' => 'DESC', 'orderby' => 'meta_value_num'); $query = new WP_Query($query_args); ?> <?php echo $args['before_widget']; ?> <?php echo $args['before_title'] . 'Top 5' . $args['after_title']; ?> <ul> <?php foreach ($query->get_posts() as $post) { setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?> "> <?php the_post_thumbnail(array(90, 60), array("class" => "posts-widget-featured-image alignleft", "style" => "margin-right: 5px ")); ?> </a> <a href="<?php the_permalink(); ?> "><?php the_title(); ?> </a> <div class="posts-widget-meta"> <?php print 'Lượt xem: ' . get_post_meta(get_the_ID(), 'luot_xem', true) . ' ,Bởi: '; the_author(); ?> </div> <div class="posts-widget-entry"> <?php echo limit_post_word(get_the_content(), 30); ?> </div> <div style="clear: left"></div> </li> <?php } wp_reset_postdata(); ?> </ul> <?php echo $args['after_widget']; }
/** * @param int $post_id * @param string $post_type * @param string $key * * @return \WP_Post */ public static function get($post_id = 0, $post_type = 'post', $key = '') { $args = ['ignore_sticky_posts' => true]; if (defined('DOING_AJAX') && DOING_AJAX) { $args['post_type'] = array_keys(static::$_post_types); if ($post_id) { $args['p'] = $post_id; $post = current((new \WP_Query($args))->get_posts()); } else { Posts::update($key, $post_id); $post = null; } } else { if (is_null(static::$_posts)) { $args['post_type'] = array_keys(static::$_post_types); $args['posts_per_page'] = -1; $ids = array_values(static::get_ids()); if (!empty($ids)) { $args['post__in'] = $ids; $the_query = new \WP_Query($args); while ($the_query->have_posts()) { $the_query->the_post(); $_post_type = get_post_type(); if (!isset(static::$_posts[$_post_type])) { static::$_posts[$_post_type] = []; } static::$_posts[$_post_type][get_the_ID()] = get_post(); } wp_reset_postdata(); $args['post__not_in'] = $ids; unset($args['post__in']); } foreach (static::$_post_types as $_post_type => $_posts_per_page) { if (!isset(static::$_posts[$_post_type])) { static::$_posts[$_post_type] = []; } $_found_posts = count(static::$_posts[$_post_type]); if ($_found_posts < $_posts_per_page) { $args['post_type'] = $_post_type; $args['posts_per_page'] = $_posts_per_page - $_found_posts; $the_query = new \WP_Query($args); static::$_query_args[$_post_type] = ['offset' => $the_query->post_count]; static::$_posts[$_post_type]['recent'] = $the_query->get_posts(); } } } if ($post_id && isset(static::$_posts[$post_type][$post_id])) { $post = static::$_posts[$post_type][$post_id]; unset(static::$_posts[$post_type][$post_id]); } else { $post_id = key(static::$_posts[$post_type]['recent']); $post = isset(static::$_posts[$post_type]['recent'][$post_id]) ? static::$_posts[$post_type]['recent'][$post_id] : null; unset(static::$_posts[$post_type]['recent'][$post_id]); } } return $post; }
/** * Get All Projects under this campaign * @return array of WP_Post of post type projects */ public function getProjects($active = TRUE) { $args = array('post_type' => 'projects', 'orderby' => 'menu_order', 'order' => 'ASC', 'tax_query' => array(array('taxonomy' => $this->taxonomy, 'field' => 'id', 'terms' => $this->id)), 'posts_per_page' => -1); if (!$active) { $args['post_status'] = 'any'; } $projects = new WP_Query($args); return $projects->get_posts(); }
protected static function deleteAllTestArticles() { $query = ['post_type' => 'post', 'meta_key' => 'automated_testing', 'meta_value' => true]; $query = new WP_Query($query); $posts = $query->get_posts(); foreach ($posts as $post) { wp_delete_post($post->ID, true); } }
/** Get WP pages list as an array. Used mainly to form a dropdowns. */ function leyka_get_pages_list() { $query = new WP_Query(apply_filters('leyka_pages_list_query', array('post_type' => 'page', 'posts_per_page' => -1))); $pages = array(0 => __('Website main page', 'leyka')); foreach ($query->get_posts() as $page) { $pages[$page->ID] = $page->post_title; } return $pages; }
/** * Ajax query for the search * * @since 1.0.0 */ public function ajax_search() { if (isset($_REQUEST['fn']) && 'get_ajax_search' == $_REQUEST['fn']) { $search_query = new WP_Query(array('s' => $_REQUEST['terms'], 'posts_per_page' => 10, 'no_found_rows' => true)); $results = array(); if ($search_query->get_posts()) { foreach ($search_query->get_posts() as $the_post) { $title = get_the_title($the_post->ID); $results[] = array('value' => $title, 'url' => get_permalink($the_post->ID), 'tokens' => explode(' ', $title)); } } else { $results[] = __('Sorry. No results match your search.', 'wp-typeahead'); } wp_reset_postdata(); echo json_encode($results); } die; }
public function getPostsFromWordpressQuery($query) { $query = new \WP_Query($query); $posts = $query->get_posts(); $toReturn = array(); foreach ($posts as $post) { $toReturn[] = $this->wordpress_post_factory->createFromWP_Post($post); } return $toReturn; }
/** * * * @since 1.0.0 * @access public * @param * @return void */ public function getRelatedTerms($taxonomy, $type, $terms, $term_tax) { $rel_terms = []; $query = new \WP_Query(['post_type' => $type, 'posts_per_page' => -1, 'tax_query' => [['taxonomy' => $term_tax, 'terms' => $terms, 'field' => 'slug']]]); $items = $query->get_posts(); foreach ($items as $item) { $term = wp_get_post_terms($item->ID, $taxonomy); array_push($rel_terms, $term[0]->name); } return array_unique($rel_terms); }
/** * Find the order active number (completed or processing ) for a given user on a course. It will return the latest order. * * If multiple exist we will return the latest order. * * @param $user_id * @param $course_id * @return array $user_course_orders */ public static function get_learner_course_active_order_id($user_id, $course_id) { $course_product_id = get_post_meta($course_id, '_course_woocommerce_product', true); $orders_query = new WP_Query(array('post_type' => 'shop_order', 'posts_per_page' => -1, 'post_status' => array('wc-processing', 'wc-completed'), 'meta_key' => '_customer_user', 'meta_value' => $user_id)); if ($orders_query->post_count == 0) { return false; } foreach ($orders_query->get_posts() as $order) { $order = new WC_Order($order->ID); $items = $order->get_items(); $user_orders = array(); foreach ($items as $item) { // if the product id on the order and the one given to this function // this order has been placed by the given user on the given course. $product = wc_get_product($item['product_id']); if ($product->is_type('variable')) { $item_product_id = $item['variation_id']; } else { $item_product_id = $item['product_id']; } if ($course_product_id == $item_product_id) { return $order->id; } } //end for each order item } // end for each order // if we reach this place we found no order return false; }