0

I'm attempting to output a custom taxonomy term details (title, description, linked posts) in a modal but can't figure out the best way to 'setup_postdata' for that term so that my template functions like the_archive_title will work.

I'm passing the term id and taxonomy to a function hooked into register_rest_route and returning the content of a PHP template. In that template I would ordinarily have access to the queried object via get_queried_object() but I'm at a loss as to how to set this up for my modal template. Thanks in advance.

1 Answer 1

0

but can't figure out the best way to 'setup_postdata' for that term so that my template functions like the_archive_title will work.

No equivalent function exists.

I'm passing the term id and taxonomy to a function hooked into register_rest_route and returning the content of a PHP template. In that template I would ordinarily have access to the queried object via get_queried_object() but I'm at a loss as to how to set this up for my modal template. Thanks in advance.

The root problem here is a misunderstanding of what setup_postdata does and how the_archive_title knows which term you're looking at.

the_archive_title, or more specifically get_the_archive_title looks at the current main post query to figure out what the title should be.

https://developer.wordpress.org/reference/functions/get_the_archive_title/

It does this with a big if else chain of conditionals:

    if ( is_category() ) {
        $title  = single_cat_title( '', false );
        $prefix = _x( 'Category:', 'category archive title prefix' );
    } elseif ( is_tag() ) {
        $title  = single_tag_title( '', false );
        $prefix = _x( 'Tag:', 'tag archive title prefix' );
    } elseif ( is_author() ) {
.... etc
    } elseif ( is_tax() ) {
        $queried_object = get_queried_object();
        if ( $queried_object ) {
            $tax    = get_taxonomy( $queried_object->taxonomy );
            $title  = single_term_title( '', false );
            $prefix = sprintf(
                /* translators: %s: Taxonomy singular name. */
                _x( '%s:', 'taxonomy term archive title prefix' ),
                $tax->labels->singular_name
            );
        }
    }

As you can see, it too uses get_queried_object. It's not getting the current term from a taxonomy/term loop, it's getting the term used to populate posts in a taxonomy archive.

With this in mind, your question no longer makes sense, and the system you're trying to use doesn't exist.

There is no equivalent function because that's not how that works.

There is a method that would sort of do what you want but it's slow and expensive, and very wasteful. AKA create a main post query for each term, adjusting its properties so that it looks like a taxonomy archive page. This means for 10 terms you'll get 10 main queries, and potentially hundreds of posts fetched.

Instead it's much more efficient to deal with term IDs, term objects, and taxonomies directly. E.g. if we look at the code and see what's inside single_term_title we can boil it down to something like this:

$title =  apply_filters( 'single_term_title', $term->name );

Or even better:

echo esc_html( $term->name );

This avoids the fundamental problem that repurposing the archive title to display the name of a term is a mistake and could have unforeseen consequences. It's much easier to display the terms name and description directly. Everything else is cruft/unnecessary.

1
  • 1
    Thanks, @Tom J Nowell for the detailed reply – that's really useful to know. I've simplified this by simply passing the term object to my template part. Jan 17 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.