is_archive() doesn’t work on public query var archive pages?

Public query vars are those available and usable via a direct URL
query.

E.g.

Read More
example.net/?category_name=tech&tag=gadgets

But doesn’t example.net/?category_name=tech&tag=gadgets show an archive? It definitely uses the archive.php template, but for some reason, the is_archive(); conditional tag doesn’t seem to work.

If is_archive(); isn’t the conditional tag to check these archives with, what is?

Related posts

2 comments

  1. Try this:

    add_action( 'template_redirect', 'my_test_if_archive' );
    
    function my_test_if_archive() {
      global $wp_query;
      $qv = array_keys( $wp_query->query );
      $archives = array('year', 'monthnum', 'day', 'w', 'm', 'author', 'post_type');
      $is_archive = ! empty( array_intersect( $qv, $archives ) );
      $is_tax = ! empty( $wp_query->tax_query->queries );
      if ( $is_archive || $is_tax ) {
        // this is an archive
      }
    }
    
  2. But doesn’t example.net/?category_name=tech&tag=gadgets show an archive?

    Yes, shows, (is_archive() returns true) however,

    It definitely uses the archive.php

    no, in this case, a category.php is used, if not found, then tag.php and if any of those files, archive.php displays your content.

    According to template hierarchy, in wordpress an archive is everything, that displays a couple of posts, then it is divided into a category archive, a tag archive, a date archive…

    EDIT

    If is_archive(); isn’t the conditional tag to check these archives with, what is?

    For this purpose are used is_category(), is_tag()… whole bunch of them Conditional Tags

Comments are closed.