Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4408,6 +4408,27 @@ public function is_tax( $taxonomy = '', $term = '' ) {
);
}

/**
* Is the query for an existing post format archive page?
*
* @since 7.2.0
*
* @param string|string[] $post_formats Optional. Post format or array of post formats
* to check against. Default empty.
* @return bool Whether the query is for an existing post format archive page.
*/
public function is_post_format_archive( $post_formats = '' ) {
$prefixed_formats = array();

if ( $post_formats ) {
foreach ( (array) $post_formats as $post_format ) {
$prefixed_formats[] = 'post-format-' . sanitize_key( $post_format );
}
}

return $this->is_tax( 'post_format', $prefixed_formats );
}

/**
* Determines whether the current URL is within the comments popup window.
*
Expand Down
29 changes: 29 additions & 0 deletions src/wp-includes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,35 @@ function is_tax( $taxonomy = '', $term = '' ) {
return $wp_query->is_tax( $taxonomy, $term );
}

/**
* Determines whether the query is for an existing post format archive page.
*
* If the $post_formats parameter is specified, this function will additionally
* check if the query is for one of the post formats specified.
*
* For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook.
*
* @since 7.2.0
*
* @global WP_Query $wp_query WordPress Query object.
*
* @param string|string[] $post_formats Optional. Post format or array of post formats
* to check against. Default empty.
* @return bool Whether the query is for an existing post format archive page.
*/
function is_post_format_archive( $post_formats = '' ) {
global $wp_query;

if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
return false;
}

return $wp_query->is_post_format_archive( $post_formats );
}

/**
* Determines whether the query is for an existing date archive.
*
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ public function assertQueryTrue( ...$prop ) {
'is_month',
'is_page',
'is_paged',
'is_post_format_archive',
'is_post_type_archive',
'is_posts_page',
'is_preview',
Expand Down
29 changes: 29 additions & 0 deletions tests/phpunit/tests/query/conditionals.php
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,35 @@ public function test_is_single_should_not_match_numeric_id_to_post_name_beginnin
$this->assertFalse( is_single( $p1 ) );
}

/**
* @ticket 23749
*/
public function test_is_post_format_archive() {
$post_id = self::factory()->post->create();
set_post_format( $post_id, 'aside' );

$this->go_to( '/?post_format=aside' );

$this->assertQueryTrue( 'is_archive', 'is_tax', 'is_post_format_archive' );
$this->assertTrue( is_post_format_archive() );
$this->assertTrue( is_post_format_archive( 'aside' ) );
$this->assertTrue( is_post_format_archive( array( 'gallery', 'aside' ) ) );
$this->assertFalse( is_post_format_archive( 'gallery' ) );
}

/**
* @ticket 23749
*/
public function test_is_post_format_archive_false_outside_a_post_format_archive() {
$post_id = self::factory()->post->create();
set_post_format( $post_id, 'aside' );

$this->go_to( get_permalink( $post_id ) );

$this->assertFalse( is_post_format_archive() );
$this->assertFalse( is_post_format_archive( 'aside' ) );
}

/**
* @ticket 44005
* @group privacy
Expand Down
Loading