本文整理汇总了PHP中WP_Query::get_queried_object方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_Query::get_queried_object方法的具体用法?PHP WP_Query::get_queried_object怎么用?PHP WP_Query::get_queried_object使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_Query
的用法示例。
在下文中一共展示了WP_Query::get_queried_object方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: maxgallery_thumb_shortcode
public function maxgallery_thumb_shortcode($atts)
{
extract(shortcode_atts(array('id' => '', 'name' => '', 'size' => '', 'width' => '', 'height' => '', 'url' => '', 'target' => ''), $atts));
$gallery_id = sanitize_text_field("{$id}");
$gallery_name = sanitize_text_field("{$name}");
$output = '';
$gallery = null;
if ($gallery_id != '' && $gallery_name != '') {
// If both given, the id wins
$gallery = get_post($gallery_id);
}
if ($gallery_id != '' && $gallery_name == '') {
// Get the gallery by id
$gallery = get_post($gallery_id);
}
if ($gallery_id == '' && $gallery_name != '') {
// Get the gallery by name
$query = new WP_Query(array('name' => $gallery_name, 'post_type' => MAXGALLERIA_POST_TYPE));
$gallery = $query->get_queried_object();
}
if (isset($gallery) && $gallery->post_status == 'publish') {
$attrs = array('size' => sanitize_text_field("{$size}"), 'width' => sanitize_text_field("{$width}"), 'height' => sanitize_text_field("{$height}"), 'url' => sanitize_text_field("{$url}"), 'target' => sanitize_text_field("{$target}"));
$output = $this->get_output($gallery, $attrs);
}
return $output;
}
示例2: is_pronamic_events_query
/**
* Check if the query is requesting event post type
*
* @param WP_Query $query
* @return boolean true if events are queried, false otherwises
*/
function is_pronamic_events_query(WP_Query $query)
{
$is_pronamic_events = false;
if ($query->is_archive() && !$query->is_tax('pronamic_event_status')) {
// Check 'post_type' var
// Note: post_type could also be an array
$post_type = $query->get('post_type');
if (!empty($post_type) && !is_array($post_type)) {
$is_pronamic_events = post_type_supports($post_type, 'pronamic_event');
}
if (!$is_pronamic_events) {
// Check queried object
$object = $query->get_queried_object();
$is_pronamic_events = isset($object, $object->name) && post_type_supports($object->name, 'pronamic_event');
}
}
return $is_pronamic_events;
}
示例3: maxgallery_shortcode
public function maxgallery_shortcode($atts)
{
extract(shortcode_atts(array('id' => '', 'name' => ''), $atts));
$gallery_id = sanitize_text_field("{$id}");
$gallery_name = sanitize_text_field("{$name}");
$output = '';
$gallery = null;
if ($gallery_id != '' && $gallery_name != '') {
// If both given, the id wins
$gallery = get_post($gallery_id);
}
if ($gallery_id != '' && $gallery_name == '') {
// Get the gallery by id
$gallery = get_post($gallery_id);
}
if ($gallery_id == '' && $gallery_name != '') {
// Get the gallery by name
$query = new WP_Query(array('name' => $gallery_name, 'post_type' => MAXGALLERIA_POST_TYPE));
$gallery = $query->get_queried_object();
}
if (isset($gallery) && $gallery->post_status == 'publish') {
$args = array('post_parent' => $gallery->ID, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'asc', 'numberposts' => -1);
$attachments = get_posts($args);
if (count($attachments) > 0) {
$options = new MaxGalleryOptions($gallery->ID);
global $maxgalleria;
$templates = $maxgalleria->get_template_addons();
foreach ($templates as $template) {
if ($template['key'] == $options->get_template()) {
$output = call_user_func($template['output'], $gallery, $attachments);
}
}
}
}
return $output;
}
示例4: array
/**
* @ticket 30623
*/
public function test_get_queried_object_with_custom_taxonomy_tax_query_with_multiple_clauses_should_return_term_object_corresponding_to_the_first_queried_tax()
{
// Don't override the args provided below.
remove_action('pre_get_posts', array($this, 'pre_get_posts_tax_category_tax_query'));
register_taxonomy('testtax2', 'post');
$testtax2_term_id = self::factory()->term->create(array('taxonomy' => 'testtax2', 'slug' => 'testtax2-slug'));
$args = array('tax_query' => array('relation' => 'AND', array('taxonomy' => 'testtax', 'field' => 'slug', 'terms' => array('tax-slug')), array('taxonomy' => 'testtax2', 'field' => 'slug', 'terms' => array('testtax2-slug'))));
$q = new WP_Query($args);
$object = $q->get_queried_object();
$expected = get_term($this->tax_id, 'testtax');
// Only compare term_id because object_id may or may not be part of either value.
$this->assertEquals($expected->term_id, $object->term_id);
}
示例5: ap_base_page_sc
public function ap_base_page_sc($atts, $content = "")
{
if (!is_question()) {
$order = get_query_var('sort');
$label = sanitize_text_field(get_query_var('label'));
if (empty($order)) {
$order = 'active';
}
//ap_opt('answers_sort');
if (empty($label)) {
$label = '';
}
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$question_args = array('ap_query' => 'main_questions', 'post_type' => 'question', 'post_status' => 'publish', 'showposts' => ap_opt('question_per_page'), 'paged' => $paged);
if ($order == 'active') {
$question_args['ap_query'] = 'main_questions_active';
$question_args['orderby'] = 'meta_value';
$question_args['meta_key'] = ANSPRESS_UPDATED_META;
$question_args['meta_query'] = array('relation' => 'OR', array('key' => ANSPRESS_UPDATED_META, 'compare' => 'NOT EXISTS'));
} elseif ($order == 'voted') {
$question_args['orderby'] = 'meta_value_num';
$question_args['meta_key'] = ANSPRESS_VOTE_META;
} elseif ($order == 'answers') {
$question_args['orderby'] = 'meta_value_num';
$question_args['meta_key'] = ANSPRESS_ANS_META;
} elseif ($order == 'unanswered') {
$question_args['orderby'] = 'meta_value';
$question_args['meta_key'] = ANSPRESS_ANS_META;
$question_args['meta_value'] = '0';
} elseif ($order == 'oldest') {
$question_args['orderby'] = 'date';
$question_args['order'] = 'ASC';
}
if ($label != '') {
$question_args['tax_query'] = array(array('taxonomy' => 'question_label', 'field' => 'slug', 'terms' => $label));
}
$question_args = apply_filters('ap_main_query_args', $question_args);
}
if (is_question()) {
$args = array('p' => get_question_id(), 'post_type' => 'question', 'post_status' => array('moderate', 'publish'));
$question = new WP_Query($args);
} elseif (is_question_tag()) {
$question_args['tax_query'] = array(array('taxonomy' => 'question_tags', 'field' => 'id', 'terms' => array(get_question_tag_id())));
$question = new WP_Query($question_args);
$tag = $question->get_queried_object();
} elseif (is_question_cat()) {
$question_args['tax_query'] = array(array('taxonomy' => 'question_category', 'field' => 'id', 'terms' => array(get_question_cat_id())));
$question = new WP_Query($question_args);
$category = $question->get_queried_object();
} elseif (is_question_tags()) {
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$per_page = ap_opt('tags_per_page');
$total_terms = wp_count_terms('question_tags');
$offset = $per_page * ($paged - 1);
$args = array('number' => $per_page, 'offset' => $offset, 'hide_empty' => false, 'orderby' => 'count', 'order' => 'DESC');
$tags = get_terms('question_tags', $args);
} elseif (is_question_categories()) {
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$per_page = ap_opt('categories_per_page');
$total_terms = wp_count_terms('question_category');
$offset = $per_page * ($paged - 1);
$cat_args = array('parent' => 0, 'number' => $per_page, 'offset' => $offset, 'hide_empty' => false, 'orderby' => 'count', 'order' => 'DESC');
$categories = get_terms('question_category', $cat_args);
} elseif (is_ap_users()) {
global $current_user_meta;
$count_args = array('fields' => 'all_with_meta', 'number' => 999999);
$user_count_query = new WP_User_Query($count_args);
$user_count = $user_count_query->get_results();
// count the number of users found in the query
$total_users = $user_count ? count($user_count) : 1;
// how many users to show per page
$per_page = ap_opt('users_per_page');
// grab the current page number and set to 1 if no page number is set
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
// calculate the total number of pages.
$total_pages = 1;
$offset = $per_page * ($paged - 1);
$total_pages = ceil($total_users / $per_page);
$order = get_query_var('sort');
if (empty($order)) {
$order = 'points';
}
$args = array('number' => $per_page, 'offset' => $offset);
if ($order == 'points') {
$args['ap_query'] = 'sort_points';
$args['meta_key'] = 'ap_points';
$args['orderby'] = 'meta_value';
$args['order'] = 'DESC';
} elseif ($order == 'newest') {
$args['orderby'] = 'date';
$args['order'] = 'DESC';
}
// The Query
$users = new WP_User_Query($args);
} elseif (is_ap_user()) {
global $current_user_meta;
global $user;
$user = get_userdata(ap_get_user_page_user());
if ($user === FALSE) {
echo '<div class="user-not-found">' . __('User not found', 'ap') . '</div>';
//.........这里部分代码省略.........
示例6: array
$args = array('paged' => $paged, 'posts_per_page' => 9, 'year' => $archive_year, 'monthnum' => $archive_month, 'order' => 'DESC', 'post_type' => 'post');
global $args_popular;
$args_popular = array('paged' => $paged, 'posts_per_page' => 9, 'year' => $archive_year, 'monthnum' => $archive_month, 'post_type' => 'post', 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC');
global $args_random;
$args_random = array('paged' => $paged, 'posts_per_page' => 9, 'year' => $archive_year, 'monthnum' => $archive_month, 'order' => 'DESC', 'post_type' => 'post', 'orderby' => 'title');
} elseif (is_year()) {
$archive_year = get_the_date('Y');
global $args;
$args = array('paged' => $paged, 'posts_per_page' => 9, 'year' => $archive_year, 'order' => 'DESC');
global $args_popular;
$args_popular = array('paged' => $paged, 'posts_per_page' => 9, 'year' => $archive_year, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC');
global $args_random;
$args_random = array('paged' => $paged, 'posts_per_page' => 9, 'year' => $archive_year, 'order' => 'DESC', 'orderby' => 'title');
} elseif (is_tag()) {
global $wp_query;
$tag = $wp_query->get_queried_object();
$current_tag = $tag->term_id;
global $args;
$args = array('paged' => $paged, 'posts_per_page' => 9, 'tag_id' => $current_tag, 'order' => 'DESC');
global $args_popular;
$args_popular = array('paged' => $paged, 'posts_per_page' => 9, 'tag_id' => $current_tag, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC');
global $args_random;
$args_random = array('paged' => $paged, 'posts_per_page' => 9, 'tag_id' => $current_tag, 'order' => 'DESC', 'orderby' => 'title');
} else {
}
?>
<?php
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query($args);
示例7: array
/**
* @ticket 31246
*/
public function test_get_queried_object_should_return_null_when_is_tax_is_true_but_the_taxonomy_args_have_been_removed_in_a_parse_query_callback() {
// Don't override the args provided below.
remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 2, array(
'taxonomy' => 'wptests_tax',
) );
$posts = $this->factory->post->create_many( 2 );
wp_set_object_terms( $posts[0], array( $terms[0] ), 'wptests_tax' );
wp_set_object_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
add_action( 'parse_query', array( $this, 'filter_parse_query_to_remove_tax' ) );
$q = new WP_Query( array(
'fields' => 'ids',
'wptests_tax' => $terms[1],
) );
remove_action( 'parse_query', array( $this, 'filter_parse_query_to_remove_tax' ) );
$this->assertNull( $q->get_queried_object() );
}
示例8: getFunctionBody
/**
* The function that is executed when called in a template
*
* @return mixed
* @author Tim Perry
*/
public function getFunctionBody()
{
$post = $this->query->get_queried_object();
return 'class="' . implode(" ", $this->getBodyClasses($post)) . '"';
}
示例9: base_template_code
public static function base_template_code($template)
{
global $wp_query, $wp_the_query, $chronosly_running, $chshortcode;
$settings = unserialize(get_option('chronosly-settings'));
$obj = get_queried_object();
$custom_post_type = get_post_type();
if (!$custom_post_type or stripos($custom_post_type, "chronosly") === FALSE) {
$obj = $wp_query->query_vars;
$custom_post_type = $obj["post_type"];
}
$archive = 0;
if (stripos($template, "ch-event") !== FALSE or stripos($template, "ch-organizer") !== FALSE or stripos($template, "ch-place") !== FALSE or stripos($template, "ch-category") !== FALSE or stripos($template, "ch-calendar") !== FALSE or self::POST_TYPE === $custom_post_type or "chronosly_category" === $custom_post_type or "chronosly_organizer" === $custom_post_type or "chronosly_places" === $custom_post_type or "chronosly_calendar" === $custom_post_type) {
Post_Type_Chronosly::get_shortcode_base();
if (!is_search()) {
$chronosly_running = 1;
}
$chshortcode .= " navigation='1'";
$chshortcode .= " before_events='1'";
$chshortcode .= " after_events='1'";
$template_page_id = $settings["chronosly-base-templates-id"];
if ($template_page_id != 0) {
if (is_archive() and $obj->taxonomy == "chronosly_category") {
$copy_fields = array('term_id', "slug", "taxonomy");
$original = array();
$original["chronosly_category"] = $obj->slug;
foreach ($copy_fields as $field) {
$original[$field] = $obj->{$field};
}
} else {
if (is_archive()) {
$archive = 1;
$original["post_title"] = $obj->labels->name;
} else {
$copy_fields = array('ID', "post_title");
$original = array();
foreach ($copy_fields as $field) {
$original[$field] = $obj->{$field};
}
}
}
global $wp_filter;
if (isset($wp_filter['pre_get_posts'])) {
$bak = $wp_filter['pre_get_posts'];
unset($wp_filter['pre_get_posts']);
} else {
$bak = false;
}
$wp_query = new WP_Query('page_id=' . $template_page_id);
$obj = $wp_query->get_queried_object();
$wrap = $obj->post_content;
global $post;
$post = $obj;
$post = is_object($post) ? $post : (object) array();
$post->post_status = 'publish';
//force it as publish
$template = get_page_template();
//fetch template before overwritting post.
if (false !== $bak) {
$wp_filter['pre_get_posts'] = $bak;
}
if ($archive) {
$post->post_title = $original["post_title"];
} else {
foreach ($copy_fields as $field) {
$post->{$field} = $original[$field];
}
if (isset($original["chronosly_category"])) {
$post->chronosly_category = $original["chronosly_category"];
}
}
$post->post_content = $obj->post_content;
$wp_query->post = $post;
$wp_the_query = $wp_query;
}
}
return $template;
}
示例10: elseif
function filter_pre_get_posts(&$query)
{
if ($query->is_main_query() and $query->is_author) {
//take over the author page
$the_ghost_author =& $this->the_ghost_author;
//get the author data
$the_ghost_author = new WP_Query(array('post_type' => 'fdp_ghost_authors', 'name' => $query->query['author_name'], 'posts_per_page' => 1));
$the_ghost_author->get_queried_object();
// echo '<pre>';
// print_r($query);
// echo '</pre><hr>';
$query->set('author_name');
$query->set('meta_key', '_fdp_ghost_author_id');
$query->set('meta_value', $the_ghost_author->queried_object_id);
// echo '<pre>';
// print_r($query);
// echo '</pre><hr>';
// exit;
} elseif ($query->is_main_query() and $query->is_single and $query->query_vars['post_type'] == 'fdp_ghost_authors') {
//add logically false condition to where filter
add_filter('posts_where', array(&$this, 'filter_the_queries_where'));
}
}
示例11: adverts_posts_results
/**
* Replaces Main Query objects.
*
* When browsing by category by default WP will display list of categories
* (depending on the theme), we do not want that, instead we want to take control
* over the page content. In order to do that this function removes main query
* list of terms and replaces them with post that holds adverts list.
*
* @param array $posts
* @param WP_Query $query
* @return array Post objects
*/
function adverts_posts_results($posts, $query)
{
if ($query->is_main_query() && $query->is_tax("advert_category")) {
$title = sprintf(__("Category: %s", "adverts"), $query->get_queried_object()->name);
$post = get_post(adverts_config('config.ads_list_id'));
$post->post_title = apply_filters("adverts_category_the_title", $title);
return array($post);
} else {
return $posts;
}
}
示例12:
function taxonomy_template($template)
{
global $wp_query;
$cat = $wp_query->get_queried_object();
$term_id = $cat->term_id;
$name = $cat->name;
$taxonomy = $cat->taxonomy;
$template_page_id = $this->get_taxonomy_template_page_id($term_id, $taxonomy);
if ($template_page_id) {
$wp_query = new WP_Query('page_id=' . $template_page_id);
$o = $wp_query->get_queried_object();
//----- without this, template selection does not gets correctly done, always default.
global $post;
$post = $o;
//------------
$template = get_page_template();
//echo "template $template<br />";
$o->post_title = $name;
$o->post_content = $this->get_taxonomy_content($term_id, $taxonomy, $o->post_content);
return $template;
}
return $this->_taxonomy_template($template);
}
示例13: unset
if (isset($query_args['pagename'])) {
$query_args['name'] = $query_args['pagename'];
unset($query_args['pagename']);
}
$suffix = 2;
$alt_post_name = $slug;
// Search for a good slug, adding an appended numeric string if necessary.
while (true) {
$query = new WP_Query($query_args);
$use_slug = true;
// If there's a post at this URL already, bail.
if ($query->have_posts()) {
$use_slug = false;
}
// Avoid collision with `tag/tagname` even if there are no posts in the taxonomy.
if ($query->get_queried_object()) {
$use_slug = false;
}
if ($post_type === 'attachment' && apply_filters('wp_unique_post_slug_is_bad_attachment_slug', false, $slug)) {
$use_slug = false;
}
if ($use_slug) {
break;
} else {
$alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
$query_args['name'] = $alt_post_name;
$suffix++;
}
}
$slug = $alt_post_name;
}
示例14: array
function taxonomy_template($template)
{
global $wp_query, $wp_the_query;
$cat = $wp_query->get_queried_object();
//handle a situation where plugins or themes takeover the main query object and does not return it to its original state.
//causing this script to load the version 1 template instead of the version 2.
if (!is_object($cat) || !property_exists($cat, 'taxonomy')) {
$taxonomies = apply_filters('rhc-taxonomies', array());
if (is_array($taxonomies) && count($taxonomies) > 0 && is_array($wp_query->query) && count($wp_query->query) > 0) {
foreach ($wp_query->query as $q_taxonomy => $q_term) {
if (array_key_exists($q_taxonomy, $taxonomies)) {
$o_term = get_term_by('slug', $q_term, $q_taxonomy);
$cat = $o_term;
break;
}
}
}
}
//---
$this->is_taxonomy = true;
$term_id = $this->term_id = $cat->term_id;
$name = $cat->name;
$taxonomy = $this->taxonomy = $cat->taxonomy;
$this->term_slug = $cat->slug;
$term_permalink = $this->term_permalink = get_term_link($cat, $taxonomy);
$this->term = $cat;
$template_page_id = $this->get_taxonomy_template_page_id($term_id, $taxonomy);
if ($template_page_id) {
global $wp_filter;
//---wpml fix part 1
$backup_filters = array();
foreach (array('posts_join', 'posts_where', 'pre_get_posts') as $hook_name) {
if (isset($wp_filter[$hook_name])) {
$backup_filters[$hook_name] = $wp_filter[$hook_name];
unset($wp_filter[$hook_name]);
}
}
//---
$wp_query = new WP_Query('page_id=' . $template_page_id);
$o = $wp_query->get_queried_object();
//----- without this, template selection does not gets correctly done, always default.
global $post;
$post = $o;
//------------
$template = get_page_template();
//---wpml fix part 2
if (!empty($backup_filters)) {
foreach ($backup_filters as $hook_name => $backup_filter) {
$wp_filter[$hook_name] = $backup_filter;
}
unset($backup_filters);
}
//---
$post_content = do_shortcode($o->post_content);
//do template shortcodes so that it flags if there is custom content
$post_content = $this->get_taxonomy_content($term_id, $taxonomy, $post_content);
/*
$post_content = str_replace("\n","",$post_content);//autop adds p tags
$post_content = str_replace("\r","",$post_content);
$post_content = str_replace("\t","",$post_content);
*/
$o->guid = $term_permalink;
$o->post_title = $name;
$o->post_content = do_shortcode($post_content);
$wp_query->post = $o;
$wp_the_query = $wp_query;
do_action('rhc_taxonomy_template_page_id_set', $template_page_id, $this->term);
return $template;
}
return $this->_taxonomy_template($template);
}
示例15: test_get_queried_object_should_return_null_for_not_exists_tax_query
/**
* @ticket 37962
*/
public function test_get_queried_object_should_return_null_for_not_exists_tax_query()
{
register_taxonomy('wptests_tax', 'post');
$q = new WP_Query(array('tax_query' => array(array('taxonomy' => 'wptests_tax', 'operator' => 'NOT EXISTS'))));
$queried_object = $q->get_queried_object();
$this->assertNull($queried_object);
}