WordPress.org

Ready to get started?Download WordPress

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
[resolved] queries by URL? (7 posts)

  1. fuentism
    Member
    Posted 2 years ago #

    Hi Rachel,

    First off this plugin is a lifesaver so thank you for developing it and putting so much thought into it—it really shows!

    I'm building a filter-type form for my home page where I want to be able to show entries of a certain custom post type by selected taxonomies.

    I was hoping to do this by just passing stuff into the URL, but am not sure how a CPT would work here...

    So for example if I were asking WP to show me categories I might have a URL like http://mysite.com/?cat=229&cat=330.

    For a regular custom taxonomy, http://mysite.com/?taxonomyname=taxonomyterm seems to work ok, but for a CPT, that redirects to the post itself, not the objects associated with it.

    Is there a way to make this work?

    http://wordpress.org/extend/plugins/cpt-onomies/

  2. Rachel Carden
    Member
    Plugin Author

    Posted 2 years ago #

    Let me look into this some and figure out the best solution. I'll get back to you ASAP. Thanks!

  3. Rachel Carden
    Member
    Plugin Author

    Posted 2 years ago #

    Here's my first question: is it actually redirecting? or just showing the post?

    Example: On my demo setup, I have an 'actors' post type and an 'actors' CPT-onomy. When I type http://localhost/?actors=tom-hanks it doesn't redirect BUT it does show the 'actors' template for the Tom Hanks' post.

  4. fuentism
    Member
    Posted 2 years ago #

    Hi Rachel,

    Wow you're quick, thank you :)

    Yes, maybe its not redirecting but just showing the post. That's the rub, it's being a post in an instance where I want it to be a t'onomy.

    The things is I want to be able to pass multiple queries into the url which include CPTs and non-CPT custom taxonomies, but treats them all like terms and returns an archive-like page of results.

    In a way, search is able to accomplish this, so if you searched 'actors' you'd get a list of posts associated with the 'actors' CPT and not land the 'actors' post itself.

  5. Rachel Carden
    Member
    Plugin Author

    Posted 2 years ago #

    Ok. What you want is totally do-able but it's going to take some customization on your part.

    First off, you're going to have to decide on your URL parameters. A simple solution might be to just add a taxonomy parameter that's set to the taxonomy/CPT-onomy name, like so:

    I have an 'actors' custom post type that is also a CPT-onomy so I would include the following parameters to define that I want to query posts that are tagged with the 'actors' taxonomy/CPT-onomy, more specifically are tagged with the 'actors' term 'tom-hanks'.

    http://mysite.com/?taxonomy=actors&actors=tom-hanks

    These URL parameters could also mean that you want 'actors' post types who are tagged with the 'actors' CPT-onomy but since this is your custom setup, you can have it mean whatever you like. To avoid confusion, you might want to rearrange it like so:

    http://mysite.com/?taxonomy=actors&term=tom-hanks

    This URL actually prints/includes the archive template because WordPress understands this to be the parameters for a taxonomy term archive page.

    You can add a post_type parameter to declare you only want particular post types who are tagged with the taxonomy term:

    http://mysite.com/?post_type=movies&taxonomy=actors&term=tom-hanks

    With me so far?

    So now that we have that covered, the next step is deciding whether or not you're fine with the page including the archive template or if you'd like WordPress to use another template (keep in mind that WordPress is not actually redirecting anything).

    But if you'd really like WordPress to use your home page template, all it takes is a little trickery. Read over all of this and let me know if you want me to keep going.

  6. fuentism
    Member
    Posted 2 years ago #

    Hi Rachel,

    Yes, this is exactly what I was asking for!

    Only caveat is, arg, it only works with one taxonomy. So,
    http://mysite.com/?post_type=movies&taxonomy=actors&term=tom-hanks
    works but not
    http://mysite.com/?post_type=movies&taxonomy=actors&term=tom-hanks&taxonomy=rating&term=turkey

    Unless I'm doing it wrong, which is 89% possible.

    But, that's ok. I'm running with the one tax for now and I guess what I need to do if I want multiple filters, so I gather, is to make a custom query from POST variables and put that on its own page.

    Anyway... thanks again for your help! You are stellar.

  7. Rachel Carden
    Member
    Plugin Author

    Posted 2 years ago #

    If you keep to just the one taxonomy and use the parameters 'taxonomy' and 'term' and 'post_type', WordPress stores the information in $wp_query. The following will show you the info:

    global $wp_query;
    echo "<pre>";
    print_r( $wp_query );
    echo "</pre>";

    Because these are default WordPress parameters, WordPress will even redirect you to the right template. It takes these parameters and recognizes that you are trying to show a taxonomy archive page and, if you have an archive.php file, it will use the archive.php template. Otherwise it will follow the template hierarchy.

    WordPress will even setup the Loop to query the correct posts.

    Once you've got your template file setup, you can access $wp_query to retrieve your URL parameters to then use, at will, to modify the Loop query as you see fit.

    If you want to add in a second taxonomy, you'll most likely have to add your own custom query variable, i.e. 'taxonomy2' or something like that, and register your custom query variable so $wp_query will recognize the parameter in the URL. Then, in your template file, access $wp_query to retrieve your parameter's info to then use at will to adjust the Loop query.

    Make sense?

    Use the following code to register a custom query variable:

    add_filter( 'query_vars', 'register_custom_query_vars' );
    
    function register_custom_query_vars( $vars ) {
       array_push( $vars, 'taxonomy2' );
       return $vars;
    }

    Let me know if you need any more help!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic