Passing variables when working with custom posts and custom taxonomies

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1445
    tipetarius2018
    Participant

    With help from others in this forum, I’m moving closer to my goal of creating workable next/previous links on custom posts that have been filtered according to a custom taxonomy. If you’re interested in background context, [my original post establishes the context](https://www.reddit.com/r/Wordpress/comments/jeti88/acf_struggles_how_do_i_pass_variables_and_handle/). (Side note: Despite the title of that post, my problem had nothing to do with the ACF plug-in.)

    Before moving to custom taxonomies, I had created eight different character list template pages that filtered the list of characters according to relevant aspecs of their identity (e.g., male, female, gay, lesbian). Since I wasn’t aware of custom taxonomies, I was trying to do this based on contents of ACF fields in the custom post.

    Following the wise advice of others in this forum, I am now using a custom taxonomy to filter characters. The next step is moving from a situation in which I have eight or more character list pages that need to be separately maintained to a single character list page that dynamically reads the context and filters accordingly.

    Technically, I have figured out how to do this with a single file. My taxonomy is called ‘identity’ and some sample identities include things like ‘bisexual’ or ‘pansexual’ or ‘transgender.’ When WordPress decides which template to use for the bisexual taxonomy, it will follow this order, moving from most specific to most general: `taxonomy-identity-bisexual.php, taxonomy-identity.php, taxonomy.php, archive.php,` and `index.php`.

    Thus, my new page for viewing lists of characters will be called taxonomy-identity.php. I have figured out that I can pass the appropriate context to the page with URL encoding in a custom variable. So, for example, this custom URL will lead to my taxonomy-identity.php page.

    `bifrostdatabase.com/identity/bisexual?context=bisexual`

    I can then read that URL and use it to filter based on the context details. The pattern works, and I could easily insert custom URLs into my menu for all eight possible identites.

    BUT… I suspect this might be a bad idea. In actuality, since I’m playing around with this in a local backup of the site, the URL looks more like this:

    `bifrost-migrate-to-windows-d1f4b162ac2f371e5873-20.local/identity/bisexual/?context=bisexual`

    Also, from past experience, it seems like custom URLs for internal page references can cause weird hiccups down the road.

    Another option, which I think makes more sense, would use the taxonomy categories to create menu items for each page. Assuming that I can just kick off the final slash and parse the word after ‘identity/’, there would be no need to use custom variables.

    Is my reasoning correct? Should I just be extracting the context from the URL based on what I know of the naming convention? Is there a better way of doing this?

    Thanks!

    #1446
    ChristopherwD
    Guest

    Awesome job, custom post types are a wonderful thing.

    So the first thing you need to get clear on is whether or not you are intending for each sexuality to actually be a **term**.

    A term is another archive within your parent taxonomy of “sexuality”.

    What this means, is that the expected behaviour of this, if we say we’re looking at bisexual for examples sake, is that there would then be say 12 “custom posts” with the category assigned as “bisexual”. Perhaps these custom posts represent characters.

    Then, when you click on a single character that is bisexual, it will show you a single page with all of their info.

    This page would then be handled by a template called something like single-character (character being the custom post type).

    If however, you are saying that you want the term “bisexual” to be it’s own page and it has various custom information then really sexuality should be a custom post type, not a term.

    Your use of template hierarchy is correct, but remember, each of these, even the single term “bisexual” would still represent an archive. Aka basically a page that lists of a bunch of posts.

    Also, you shouldn’t need to use query parameters (?context=bisexual)

    And yes, the objects exist on the templates automatically, you don’t need to pass anything.

    How you are accessing it in the comments below is correct, WordPress will automatically setup wp_query for you if you are working in taxonomy/post templates.

    #1447
    tipetarius2018
    Guest

    News flash: I have realized that this block of code will help me identify the context for a taxonomy page. So, if I have this on the taxonomy-identity.php page and a user selects the ‘female’ taxonomy option, this code will allow me to store the current context in a variable.

    <?php
    $tax = $wp_query->get_queried_object();
    $current_context = ”. $tax->name . ”;
    echo $current_context;
    ?>

    This is awesome, but I’m confused about what is happening here because I thought I had to initiate the query within my PHP file. When someone loads the page [bifrostdatabase.com/identity/female](https://bifrostdatabase.com/identity/female), does WordPress automatically pass an object containing all of the taxonomy details to taxonomy-identity.php? And, if it is passing a taxonomy object automatically, is that object already filtered based on the selected term?

    Hope these aren’t foolish questions. I figure that documenting this challenge in these Reddit posts might someday be useful to someone else who is trying to do something similar.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.