get_results( "SELECT * FROM $wpdb->postmeta AS pm, $wpdb->posts AS ps WHERE ps.ID = pm.post_id AND pm.meta_key = 'mf_page_type' AND pm.meta_value = '{$type}' " . $where . ($count?" LIMIT {$count}":"") . ($offset?" OFFSET {$offset}":"")); self::prepare_posts($posts,$type,$extract_image); return $posts; } /* * Return an array of posts with the specified tag * * @param string $tag tag to filter * @param string $type custom field type * @param int $count max number of elements (0 for all) * @param int $offset start from this offset (user for pagination) * @param int $extract_image define if we should automatically extract an image * @param string $where optional mysql where */ public static function get_posts_by_tag($tag, $type, $count = 0, $offset = 0, $extract_image = false, $where = ''){ global $wpdb; // get the id on tag $tag = $wpdb->get_results( "SELECT ts.term_id FROM $wpdb->terms AS ts, $wpdb->term_taxonomy AS tx WHERE ts.term_id = tx.term_id AND tx.taxonomy = 'post_tag' AND ts.name = '$tag'"); if(!isset($tag[0])) return false; $tag_id = $tag[0]->term_id; // make the biiig query $posts = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta AS pm, $wpdb->posts AS ps, $wpdb->term_relationships AS ts WHERE ps.ID = pm.post_id AND pm.post_id = ts.object_id AND ts.term_taxonomy_id = $tag_id AND pm.meta_key = 'mf_page_type' AND pm.meta_value = '{$type}' " . $where . ($count?" LIMIT {$count}":"") . ($offset?" OFFSET {$offset}":"")); self::prepare_posts($posts,$type,$extract_image); return $posts; } /* * Adjust posts taken from the DB * * @param array $posts array of posts * @param string $type custom field type * @param int $extract_image define if we should automatically extract an image */ public static function prepare_posts(&$posts, $type, $extract_image = false){ // load custom fields $custom_fields = self::get_custom_fields($type); foreach($posts as $key => $post){ // fix the content $posts[$key]->post_content = apply_filters('the_content', $post->post_content); // add custom fields foreach($custom_fields as $field){ $posts[$key]->$field = get_post_meta($post->ID, $field, true); } // if needed, extract the first image if($extract_image){ $posts[$key]->post_image = self::extract_image($post->post_content); } } } /* * Return a single post with custom fields and extra options * Can be used inside the Loop (omit id or set it to null) * * @param int $id post id optional inside the Loop * @param int $extract_image define if we should automatically extract an image */ public static function get_post($id = null, $extract_image = false){ // get the ID from the Loop if(is_null($id)) $id = $GLOBALS['post']->ID; $type = self::get_type($id); $posts = self::get_posts($type,1,0,$extract_image, "AND ps.ID = $id"); return $posts[0]; } /* * Return an array of similar posts, using tags to compare * Use inside the Loop * * @param integer $count max number of elements (0 = all) * @param boolean $extract_image define if we should automatically extract an image * @param integer $id optional if inside the loop */ public static function get_similars($count = 0, $extract_image = false, $id = null){ global $wpdb; // get the ID from the loop if(is_null($id)) $id = $GLOBALS['post']->ID; $type = self::get_type($id); $tags = wp_get_post_tags($id, array('fields' => 'ids')); // get all the posts (except himslef) with the same type $posts = self::get_posts($type,0,0,$extract_image, "AND ps.ID != $id"); // add tags foreach($posts as $key => $post){ $post->tags = wp_get_post_tags($post->ID, array('fields' => 'ids')); // calculate tags similarity $post->tags_similarity = count(array_intersect($tags, $post->tags)); } function custom_sort($a,$b){ return $b->tags_similarity - $a->tags_similarity; } // sort posts by tags similarity usort($posts, 'custom_sort'); // return all if(!$count) return $posts; // return only a portion else return array_slice($posts,0,$count); } /* * Return the type of the current post * If the $id is not set, use inside the Loop * * @param int $id optional */ public static function get_type($id){ global $wpdb; // get the ID from the loop if(is_null($id)) $id = $GLOBALS['post']->ID; // make the query $posts = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta AS pm WHERE pm.post_id = $id AND pm.meta_key = 'mf_page_type'"); // return the type return count($posts) && isset($posts[0]->meta_value) ? $posts[0]->meta_value : 'post'; } /* * Return the number of total posts of the specified type * * @param string: custom field type */ public static function get_posts_count($type){ global $wpdb; // make the query $results = $wpdb->get_results( "SELECT COUNT(*) FROM $wpdb->postmeta AS pm, $wpdb->posts AS ps WHERE ps.ID = pm.post_id AND pm.meta_key = 'mf_page_type' AND pm.meta_value = '{$type}'", ARRAY_A); return (int)array_shift($results[0]); } /* * Return an array of custom boxes for a specific type * * @param string: custom field type * @param boolean: if false return complete objects */ public static function get_custom_boxes($type, $only_name = true){ $custom_boxes = array(); // look if there are custom boxes linked to the specified type foreach(self::$types as $t){ if(strcasecmp($t['name'], $type) == 0){ foreach($t['visible_boxes'] as $vb){ // check if it's a custom box if((isset(self::$boxes[$vb]))){ $custom_boxes[] = $only_name ? $vb : self::$boxes[$vb]; } } } } return $custom_boxes; } /* * Return an array of custom fields for a specific type * * @param string: custom field type * @param boolean: if false return complete objects */ public static function get_custom_fields($type, $only_key = true){ // load custom boxes $custom_boxes = self::get_custom_boxes($type, false); $custom_fields = array(); // loop boxes foreach($custom_boxes as $custom_box){ if(isset($custom_box['field']) && !empty($custom_box['field'])){ if($only_key){ foreach($custom_box['field'] as $field){ $custom_fields[] = $field['key']; } }else{ $custom_fields = array_merge($custom_fields, $custom_box['field']); } } } return $custom_fields; } /* * Extract the first image src inside some html content * * @param html $content */ public static function extract_image($content){ $pattern = '~~'; preg_match($pattern, $content, $match); return $match[1]; } }