$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to read the value of a WordPress $query associative array (hash) key|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to read the value of a WordPress $query associative array (hash) key

matteradmin9PV0评论

New to WordPress programming (coming from a more conventional environment) and trying to understand some of its "unique" qualities.

There is a directory page on our website and this code resides, that is in functions.php, tweaks the results if condition is true.

if( $query->is_post_type_archive( 'directory' ) ){ 
    ...//do stuff
}

I would like to know how to access the "value of is_post_type_archive that is "directory." When I use test for the value...

var_dumb($query->is_post_type_archive());

..I get bool(true) which makes sense. But how/where is the value "directory" stored/passed/accessed?

New to WordPress programming (coming from a more conventional environment) and trying to understand some of its "unique" qualities.

There is a directory page on our website and this code resides, that is in functions.php, tweaks the results if condition is true.

if( $query->is_post_type_archive( 'directory' ) ){ 
    ...//do stuff
}

I would like to know how to access the "value of is_post_type_archive that is "directory." When I use test for the value...

var_dumb($query->is_post_type_archive());

..I get bool(true) which makes sense. But how/where is the value "directory" stored/passed/accessed?

Share Improve this question asked Nov 9, 2018 at 15:15 breadwildbreadwild 3916 silver badges22 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

First up, the first thing you should do is read the developer docs. This question touches on a lot of topics, and it's not going to be possible to explain them all in one answer.

Anyway, directory would be a Custom Post Type registered by the theme or a plugin.

When a post type is registered the developer can tell WordPress what the URL path for its archive should be.

WordPress will then create a rewrite rule so that when a user visits that URL, WordPress receives a parameter that tells it that the user is requesting the archive for that post type.

When the user visits that URL WordPress will query the posts it needs to show for that post type archive. It will do that with a WP_Query() object. As part of this query WordPress will set the is_post_type_archive property of that object to true, and the post_type property to directory.

Developers can change the behaviour of post queries using the pre_get_posts filter. Since WordPress can run more than one post query on any given request, any functions used on this filter will receive the current WP_Query object as a parameter. The developer can then choose to modify the only the main query for the post type archive by checking if $query->is_post_type_archive( 'directory' ) is true for the current query.

You found a regular WordPress object, $query, which is well documented in the Code Reference on its own page:

WP_Query::is_post_type_archive( mixed $post_types = '' )

Is the query for an existing post type archive page?

Description

Parameters

$post_types (mixed) (Optional) Post type or array of posts types to check against.

Default value: ''

The source code is given there as well. So what does is_post_type_archive($post_types) do?

  • is_post_type_archive() returns true if the current page is a post type archive of any kind

  • is_post_type_archive('foo') returns true if the current page if a post type archive for the (custom) post type foo (could be post, page, etc)

  • is_post_type_archive(['foo', 'bar']) returns true if the current page is a post type archive for any of those (custom) post types foo, bar.

Post a comment

comment list (0)

  1. No comments so far