/** * Make any needed modifications to the main query * via pre_get_posts for the homepage or frontpage * * @since 2.3.0 * * @param WP_Query $q Current WP_Query object at the time of pre_get_posts */ public function pre_get_posts($q) { if (!$q->is_main_query() || !$q->is_home() && !$q->is_page() && !$q->is_archive()) { return; } // Static frontpage if ($q->is_page() && get_option('show_on_front') == 'page' && get_option('page_on_front') == $q->get('page_id')) { $templates = apply_filters('themeblvd_paginated_templates', array('template_list.php', 'template_list.php', 'template_builder.php')); $template = get_post_meta($q->get('page_id'), '_wp_page_template', true); if (in_array($template, $templates) && isset($q->query['paged'])) { $q->set('paged', $q->query['paged']); } } // Adjust posts_per_page if framework is in grid mode if ($q->is_archive() || $this->is_blog($q)) { // Check to make sure we're in grid mode if (themeblvd_is_grid_mode()) { $key = 'archive'; if ($this->is_blog($q)) { $key = 'index'; } $columns = themeblvd_get_option("{$key}_grid_columns"); if (!$columns) { $columns = apply_filters('themeblvd_default_grid_columns', 3); } $rows = themeblvd_get_option("{$key}_grid_rows"); if (!$rows) { $rows = apply_filters('themeblvd_default_grid_rows', 4); } // Posts per page = $columns x $rows $q->set('posts_per_page', $columns * $rows); } } // Exclude any categories from posts page if ($this->is_blog($q)) { $cat = ''; if (themeblvd_is_grid_mode()) { $exclude = themeblvd_get_option('grid_categories'); } else { $exclude = themeblvd_get_option('blog_categories'); } if ($exclude) { foreach ($exclude as $key => $value) { if ($value) { $cat .= sprintf('-%s,', $key); } } } if ($cat) { $cat = themeblvd_remove_trailing_char($cat, ','); $q->set('cat', $cat); } } // Apply pagination fix when homepage custom layout // set over home "posts page" if (defined('TB_BUILDER_PLUGIN_VERSION') && $q->is_home() && 'custom_layout' == themeblvd_get_option('homepage_content')) { // Layout info $kayout_name = themeblvd_get_option('homepage_custom_layout'); $layout_post_id = themeblvd_post_id_by_name($kayout_name, 'tb_layout'); if ($layout_post_id) { $elements = get_post_meta($layout_post_id, 'elements', true); } // Loop through elements and look for that single // paginated element (there can only be one in a layout). if (!empty($elements) && is_array($elements)) { foreach ($elements as $area) { if (!empty($area) && is_array($area)) { foreach ($area as $element) { switch ($element['type']) { case 'post_grid_paginated': if (!empty($element['options']['rows']) && !empty($element['options']['columns'])) { $posts_per_page = intval($element['options']['rows']) * intval($element['options']['columns']); } $q->set('posts_per_page', $posts_per_page); break; case 'post_list_paginated': if (isset($element['options']['source']) && 'query' == $element['options']['source']) { if (!empty($element['options']['query'])) { $custom_q = wp_parse_args(htmlspecialchars_decode($element['options']['query'])); } if (isset($custom_q['posts_per_page'])) { $q->set('posts_per_page', $custom_q['posts_per_page']); } } else { if (!empty($element['options']['posts_per_page'])) { $q->set('posts_per_page', $element['options']['posts_per_page']); } } break; } } } } } } do_action('themeblvd_pre_get_posts', $q, $this); }
/** * Fix for events page rules if permalinks are disabled, unfortunatelly not too elegant. */ public function alter_event_page_request($request) { if (!is_admin() && !get_option('permalink_structure') && isset($request['page_id'])) { $is_event_archive = false; // WPML & Polylang if (function_exists('icl_object_id') && defined('ICL_LANGUAGE_CODE')) { if ((int) $request['page_id'] === (int) icl_object_id(Events_Maker()->options['general']['pages']['events']['id'], 'page', true, ICL_LANGUAGE_CODE)) { $is_event_archive = true; } } elseif ((int) $request['page_id'] === (int) Events_Maker()->options['general']['pages']['events']['id']) { $is_event_archive = true; } // is requested page an event archive page? if ($is_event_archive === true) { // the query isn't run if we don't pass any query vars $query = new WP_Query(); $query->parse_query($request); if ($query->is_page()) { unset($request['page_id']); $request['post_type'] = 'event'; } } } return $request; }
/** * Alternates request * * @param array $request * @return array */ public function alter_request($request) { if (is_admin()) { return $request; } $mapping = array(); foreach ($this->get_post_types() as $post_type) { $page_id = (int) g1_get_theme_option('post_type_' . $post_type, 'page_for_posts'); if ($page_id) { $mapping[$post_type] = $page_id; } } foreach ($mapping as $post_type => $page_id) { // WPML fallback if (function_exists('icl_object_id')) { $page_id = absint(icl_object_id($page_id, 'page', true)); } if ($page_id) { // The query isn't run if we don't pass any query vars $query = new WP_Query(); $query->parse_query($request); // Change request from page to post type archive if ($query->is_page()) { if (absint($query->get('page_id')) === $page_id || strlen($query->get('pagename')) && absint($query->get_queried_object_id()) === $page_id) { unset($request['page']); unset($request['page_id']); unset($request['pagename']); $request['post_type'] = $post_type; } } } } return $request; }
/** * The rule of this ordering is: from the most specific to the least. * Most of the default WP Template Hierarchy is the same, but not all is followed. * * For the full example of our lookup order plesase follow to: * * For the default WP hierarchy follow to: * http://codex.wordpress.org/Template_Hierarchy * * @param WP_Query $wp_query * @return array */ protected function defineLookupOrder(\WP_Query $wp_query) { $result = []; if (!$wp_query) { return $result; } // prepare vars $post = !empty($wp_query->posts) ? $wp_query->posts[0] : false; $post_type = $post ? $post->post_type : false; $post_slug = $post ? $post->post_name : false; $query_post_type = $wp_query->query_vars['post_type']; if (is_array($query_post_type)) { // it's not usual to have multiple post types on a rewrite rule // but even if there is, it's extremely inconsistent to rely on // a template name with multiple post types // if that's the case, the user will have to alter the template // order manually $query_post_type = false; } // start the template hierarchy build up if ($wp_query->is_404()) { // 404-[post-type] // 404 if ($query_post_type) { $result[] = '404-' . $query_post_type; } $result[] = '404'; } elseif ($wp_query->is_search()) { // search // archive $result[] = 'search'; $result[] = 'archive'; } elseif ($wp_query->is_front_page()) { // if is page on front: // front-page // page // singular // if is posts on front: // front-page // home // archive-[post-type] // [post-type] // archive $result[] = 'front-page'; if ($post_type) { if ($post_type !== 'page') { $result[] = 'home'; $result[] = 'archive-' . $post_type; $result[] = $post_type; $result[] = 'archive'; } else { $result[] = 'page'; $result[] = 'singular'; } } } elseif ($wp_query->is_home()) { // home // archive-[post-type] // [post-type] // archive $result[] = 'home'; if ($post_type) { $result[] = 'archive-' . $post_type; $result[] = $post_type; $result[] = 'archive'; } // for now this is not needed, test more // } elseif ($wp_query->is_post_type_archive()) { // $result[] = 'archive-'.$query_post_type; // $result[] = $query_post_type; // $result[] = 'archive'; } elseif ($wp_query->is_author()) { // author-[user-login] // author-[user-nicename] // author // archive if ($author = get_userdata($post->post_author)) { $result[] = 'author-' . $author->data->user_login; if ($author->data->user_login !== $author->data->user_nicename) { $result[] = 'author-' . $author->data->user_nicename; } } $result[] = 'author'; $result[] = 'archive'; } elseif ($wp_query->is_tax() || $wp_query->is_tag() || $wp_query->is_category()) { // taxonomy-[taxonomy]-[term-slug] // taxonomy-[taxonomy] // taxonomy-[post-type] // taxonomy // archive-[post-type] // [post-type] // archive $term = get_queried_object(); if (!empty($term->slug)) { $result[] = 'taxonomy-' . $term->taxonomy . '-' . $term->slug; $result[] = 'taxonomy-' . $term->taxonomy; } if ($query_post_type) { $result[] = 'taxonomy-' . $query_post_type; } $result[] = 'taxonomy'; if ($query_post_type) { $result[] = 'archive-' . $query_post_type; $result[] = $query_post_type; } $result[] = 'archive'; } elseif ($wp_query->is_date()) { // date-[post-type] // date // archive-[post-type] // [post-type] // archive if ($query_post_type) { $result[] = 'date-' . $query_post_type; } $result[] = 'date'; if ($query_post_type) { $result[] = 'archive-' . $query_post_type; $result[] = $query_post_type; } $result[] = 'archive'; } elseif ($wp_query->is_archive()) { // archive-[post-type] // [post-type] // archive if ($query_post_type) { $result[] = 'archive-' . $query_post_type; $result[] = $query_post_type; } $result[] = 'archive'; } elseif ($wp_query->is_page()) { // page-[parent-slug]-[post-slug] // page-[post-slug] // [page-template-name] // page // singular if ($post->post_parent) { if ($parent_slug = get_slug($post->post_parent)) { $result[] = 'page-' . $parent_slug . '-' . $post_slug; } } $result[] = 'page-' . $post_slug; // page templates can have their unique names, let's add them before the fallback if ($page_template_name = get_page_template_name($post->ID)) { $result[] = $page_template_name; } $result[] = 'page'; $result[] = 'singular'; } elseif ($wp_query->is_attachment()) { // single-attachment-[slugfied-long-mime-type] // single-attachment-[slugfied-short-mime-type] // single-attachment // attachment // single // singular // slugfied-long-mime-type = image-jpeg // slugfied-short-mime-type = jpeg if (!empty($post->post_mime_type)) { $result[] = 'single-attachment-' . \Bond\to_slug($post->post_mime_type); $mime = explode('/', $post->post_mime_type); if (count($mime) > 1) { $result[] = 'single-attachment-' . \Bond\to_slug($mime[1]); } $result[] = 'single-attachment-' . $mime[0]; } $result[] = 'single-attachment'; $result[] = 'attachment'; $result[] = 'single'; $result[] = 'singular'; } elseif ($wp_query->is_single()) { // single-[post-type]-[post-slug] // single-[post-type] // [post-type] // single // singular $result[] = 'single-' . $post_type . '-' . $post_slug; $result[] = 'single-' . $post_type; $result[] = $post_type; $result[] = 'single'; $result[] = 'singular'; } // everything is handled, allow a filter and go $result = apply_filters($this->hooks_prefix . '/lookup_order', $result); return $result; }