0

EDIT: I am going to simplify this whole question. If I have domain.com/something/{$variable} how do I retrieve that $variable to use in a query within my template. This is really the extent of the issue.

I am having a lot of difficulties with what I thought should have been a very basic query. But wordpress's taxonomy system is making things difficult.

I am utilizing bricks, metabox, and JET Engine plugins, which will allow me to properly create M:M relations between a HOBBY CPT and about a dozen others. One will be articles.

I can set up the database, I can set up the single post templates on both sides, And automatically generate a link so that my Hobby page, displays domain.com/{child_cpt}s/for/{HOBBY}

Ie domain.com/articles/for/baseball or domain.com/photos/for/baseball, etc.

or i can do domain.com/baseball/articles and individual articles would just use a domain.com/article/article_slug etc

I can do all that.

What I can't figure out is how to get the index page of child_cpts based on the defined {Hobby-slug} in the url.

Everyone keeps telling me to use categories. But that would require me to enter the 1000+ topics once as a post type and again as a taxonomy. That is stupid. I just need to pull the variable. that follows /for/{$hobby}

I've seen a few posts on here that look like they're probably what I need, but I dont understand the code structure enough to know how to modify it for my needs. Is there something I can enter in a code block for my bricks builder template to pull the variable from the url? Once I get that variable, I can take it from there.

Get custom post type slug for an archive page

I am trying to set things up in dedicated DB tables for optimization, but some post types may just stay in the default post table.

But a standard pivot table is created for my M:M relations.

enter image description here

EDIT:

I manually created a menu as I dont think wordpress has a default solution for dynamic tags.

<ul>
    <li id="menu-item"
        class="menu-item menu-item-type-custom menu-item-object-custom  bricks-menu-item">
        <a href="/hobby/<?php $hobby; ?>">Hobby</a></li>
    <li id="menu-item"
        class="menu-item menu-item-type-custom menu-item-object-custom  bricks-menu-item">
        <a href="/articles/for/<?php $hobby; ?>">Articles</a></li>
    <li id="menu-item"
        class="menu-item menu-item-type-custom menu-item-object-custom  bricks-menu-item">
        <a href="/groups/for/<?php $hobby; ?>">Groups</a></li>
    <li id="menu-item"
        class="menu-item menu-item-type-custom menu-item-object-custom  bricks-menu-item">
        <a href="/books/for/<?php $hobby; ?>">Books</a></li>
    <li id="menu-item"
        class="menu-item menu-item-type-custom menu-item-object-custom  bricks-menu-item">
        <a href="/resources/for/<?php $hobby; ?>">Resources</a></li>
    <li id="menu-item" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item bricks-menu-item">
        <div class="brx-submenu-toggle icon-right">
            <a href="#">Media</a>
            <button aria-expanded="false" aria-label="Media Sub menu">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 12" fill="none" class="menu-item-icon"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg></button></div>
        <ul class="sub-menu">
            <li id="menu-item" class="menu-item menu-item-type-custom menu-item-object-custom menu-item bricks-menu-item">
                <a href="/photos/for/<?php $hobby; ?>">Photos</a></li>
            <li id="menu-item" class="menu-item menu-item-type-custom menu-item-object-custom menu-item bricks-menu-item">
                <a href="/video/for/<?php $hobby; ?>">Video</a></li>
            <li id="menu-item" class="menu-item menu-item-type-custom menu-item-object-custom menu-item bricks-menu-item">
                <a href="/audio/for/<?php $hobby; ?>">Audio</a></li>

        </ul>
    </li>
</ul>

If i use the code provided by sally I would need to add $hobby=hobby_filter();

But i am not sure what I am supposed to do with the rewrite rule. It may be needed, and i understand what the code is doing i think, but I'm not sure how to apply it to my situation.

I think a get_query_var() method is needed somewhere so the value can be utilized within the bricks or jet engine query builder.

If I were doing this with Laravel Id have a route as

Route::get('/articles/for/{hobby:slug}', 'ArticlesFrontendController@index')->name('article.browse');

and a corresponding controller with

   public function index(Hobby $hobby)
    {

        $articles = Article::where('status','published')->whereHas('hobbies', function ($query) use ($hobby) {
            $query->where('hobby_id',$hobby->id);
        })->with(['users:id,username',
                  ...])->get();
        return view('articles.frontend.index', compact('articles',  'hobby'));
    }

where the hobby slug gets passed to pull the proper object

This would produce the query "select articles where hobby = $hobby "

Then I simply loop through the $articles in the view.

The hobby post type itself has three taxonomies and a dozen custom fields already attached to it, so it has to be a custom post type.

WordPress in general is missing any good way to relate posts to each other. I provided this suggested update, but who knows if they will consider it.

The best way to think of this is as a group or a business who may have members, reviews, departments, clients, etc related to it. It is easy enough to put related items links on the parent page itself, but trying to get the related items on a separate page is proving difficult.

Goal from Laravel version: enter image description here

enter image description here

1
  • "But that would require me to enter the 1000+ topics once as a post type and again as a taxonomy" - how so, or can you show/give some examples?
    – Sally CJ
    Commented Jul 16, 2023 at 11:50

1 Answer 1

0

As for what you asked:

If I have domain.com/something/{$variable} how do I retrieve that $variable

In your case, you would want to do these:

  1. Add that variable to the public query variables which are automatically parsed by WordPress, e.g. from the URL of the current page.

  2. Add a custom rewrite rule which will let WordPress know what should be displayed for that URL.

  3. Use get_query_var() to get the value of that variable.

Here's a working example for the domain.com/<post type>/for/<hobby> structure:

  1. Add the variable, which I named it for_hobby, using the query_vars hook:

    add_filter( 'query_vars', 'my_custom_query_vars' );
    function my_custom_query_vars( $vars ) {
        // Add the variable.
        $vars[] = 'for_hobby';
    
        return $vars;
    }
    
  2. Add the custom rewrite rule, using add_rewrite_rule():

    add_action( 'init', 'my_custom_rewrite_rules' );
    function my_custom_rewrite_rules() {
        // Your post types, which are used in the permalink URL.
        $post_types = array( 'articles', 'photos' );
    
        add_rewrite_rule(
            // Regular expression (RegEx) pattern for matching variables in the URL.
            '(' . implode( '|', $post_types ) . ')/for/([^/]+)(?:/page/(\d+)|)/?$',
    
            // The query variables which are passed to WP_Query.
            'index.php?post_type=$matches[1]&for_hobby=$matches[2]&paged=$matches[3]',
    
            'top'
        );
    }
    

    That (?:/page/(\d+)|) and &paged=$matches[3] is for paginated requests, e.g. page 2 at domain.com/articles/for/baseball/page/2.

    PS: Don’t forget to flush the rewrite rules! (Or re-save your permalinks) Just visit the Permalink Settings admin page without having to do anything else on that page.

  3. To get the variable's value, use get_query_var( 'for_hobby' ).

But, are you absolutely sure you do not want to use categories?

Because a taxonomy can be attached to one or more post types, so you can use just one Hobby taxonomy containing various topics which can be shared among the different post types. E.g.

  1. Create Hobby as a taxonomy and attach the Hobby taxonomy to your post types (Articles and Photos).

  2. Create Baseball as a term/category in the Hobby taxonomy, and just assign your posts to that category.

    That way, domain.com/hobby/baseball will display Articles/Photos assigned to the Baseball category.

    What's more, you can pretty easily filter the posts by post type via the post_type parameter:

    • domain.com/hobby/baseball?post_type=articles — displays only the Articles in the Baseball Hobby

    • domain.com/hobby/baseball?post_type=photos — displays only the Photos in the Baseball Hobby

16
  • sorry for not replying sooner, i did not get (or didnt see) that I got any replies. Regarding categories, yes i am absolutely sure, because the hobbies post type has a whole host off additional information that goes with it including 2-3 other taxonomies such as categories, and ratings to filter the 950 hobbies i have defined.
    – Jon C
    Commented Aug 10, 2023 at 3:08
  • But this relationship would also be applicable on a thousand other types of relations... Relators have properties, Classes have students. etc. It is easy enough to make the relationship if i include the related post on the same page as the parent, but if i want to view the related items on a separate page, this is proving to be very difficult within WordPress. I'm going to try to figure out where to put your suggested code so i can make it work. This one little thing is stalling my whole project
    – Jon C
    Commented Aug 10, 2023 at 3:13
  • You can add the code as part of your theme (add to the functions.php file), or just create a simple plugin and then put the code in the main plugin file. Also, if you believe it's better using multiple CPTs than creating them as taxonomies/categories, then just use multiple CPTs. Just remember that for grouping similar posts together, using a taxonomy is the preferred approach, because it'd be easy and fast when you need to query for related posts by the post category/tag/term.
    – Sally CJ
    Commented Aug 10, 2023 at 4:47
  • "the hobbies post type has a whole host off additional information" - they could be added as custom fields, i.e. post meta, but if you need to query/search/filter posts by any of these additional info, then as I said, taxonomy is better (or more performant) for that purposes. And note that you can create private taxonomies and then add a meta box on the post editing screen with form fields for editing these taxonomies (which represent those additional info) instead of actually displaying/editing the post meta.
    – Sally CJ
    Commented Aug 10, 2023 at 5:03
  • Ok i am really trying not to debate cause i do appreciate your help. But i come from working with Laravel where every model (custom post type) is represented in its own table. So creating relations between models is standard practice. Taxonomies are meant to classify models. Not relate them. I can't explain this fully in a comment so i need to add additional responses. ..
    – Jon C
    Commented Aug 12, 2023 at 1:26

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.