• Resolved pmdci

    (@pmdci)


    Hi,

    I started a discussion about showing custom post types on a custom page here, but since then I ran into another issue, which I believe is a separate matter so here it goes:

    In my website I am currently using:
    1. Posts for property adverts.
    2. A custom post type called partner for our partners nationwide.
    3. Categories for property types (eg: House, Flat, Condo, etc).
    4. A custom taxonomy type called city for listing our cities (eg: Rome, Milan, Madrid, etc). This taxonomy is used by BOTH posts (properties) and partners.

    5. index.php only shows posts of the type post (the default post type used for property adverts).
    6. page-partners.php shows the posts of the type partner (the custom type I created for our partners nationwide).

    So far so good. This is all working fine. However, I noticed a little nuisance with queries being passed using the URL:

    When I browse the categories of my site (eg: /flats/ or /houses/, I can use a query in the URL for my custom taxonomy called city and it works like a charm. But with my custom post type partner, it doesn’t work.

    For example, if I add to my URL: /house/?city=rome
    — WORKS: It will return to me all the posts in the category house that only have the city Rome assigned to them (rome is a term of my custom taxonomy called city).

    However, if I try the above example of using a query in the URL, it doesn’t work.

    For example, if I add to my URL: /partners/?city=rome
    — WON’T WORK: Instead of returning all the posts of type partner that only have the city Rome assigned to them, I still returns me all the partners regardless of their city.

    Any ideas of what I might be doing wrong?

    Thanks in advance,
    P.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter pmdci

    (@pmdci)

    One day wasted on this and I still haven’t managed to fix this 🙁

    I am sure there must be a way!?

    I even tried to hardcode the URL like: /index.php?pagename=partners&cidade=rome
    Or /partners/?city=rome or /partners/&city=rome … doesn’t matter… nothing works! 🙁

    However if I do this in any other page for my default post type, it works! There must be something that I am doing wrong…

    I found an article here which I think is about what I am trying to do, so based on their tutorial’s code, I came out with the code below.

    The idea is that the page called partners (which shows posts of the type partner) can accept the URL query for the custom taxonomy called city.

    NOTE: partners (in plural) is a page name. The custom taxonomy is partner (in singular form). With the exception of not being able to query posts of the type partner based on the taxonomy city, everything else is working like it should.

    Here is the code I came up with:

    <?php
    // my desperate attempt of passing URL queries to post type PARTNER
    function add_query_vars($aVars) {
        $aVars[] = "city";    // represents the name of the product category as shown in the URL
        return $aVars;
    }
    add_filter('query_vars', 'add_query_vars'); // hook add_query_vars function into query_vars
    ?>
    <?php
    function add_rewrite_rules($aRules) {
        $aNewRules = array('partners/([^/]+)/?$' => 'index.php?pagename=partners&city=$matches[1]');
        $aRules = $aNewRules + $aRules;
        return $aRules;
    }
    
    // hook add_rewrite_rules function into rewrite_rules_array
    add_filter('rewrite_rules_array', 'add_rewrite_rules');
    ?>

    But so far I have no luck… Am I on a lost cause? I would appreciate any input from you guys.

    Thanks in advance,
    P.

    Thread Starter pmdci

    (@pmdci)

    Nevermind. I found the solution and I notified everyone who helped me by email.

    Thank you guys,
    P.

    Thread Starter pmdci

    (@pmdci)

    For those of you coming to this post on RLM’s SEO blog:
    rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url

    The way the solution I found works with “ugly” links only so far. For example:

    /index.php?pagename=partner&city=rome

    As for how I achieved this, in my page-partners.php I added the following code before my loop:

    <?php
    $paged = get_query_var("paged");
    $url = $_SERVER['REQUEST_URI'];
    parse_str($url, $query);
    query_posts("&post_type=partner&orderby=title&order=asc&city=" . $query['city'] . "&paged=" . $paged);
    global $more;
    $more = 0;
    ?>

    In the above code, replace:
    * partner: for the post-type you want to query. In my case partner is a custom post type.
    * city: this is your custom taxonomy. Replace city with the taxonomy you want to use. Note that the custom taxonomy (city) is mentioned two times in the code. The first time (&city=) is to add the ability to query the post type with the city taxonomy. The second time (". $query['city'] . ") is to append to &city= the value from the query city returned by the URL.

    $query is an array holding all the queries from the URL, so what we doing is catching the value for city, which is the taxonomy term.

    HERE IS THE CATCH
    For pretty links (that is, using permalinks), so far I haven’t been successful in doing something such as /partners/city/rome (this would be amazing) or /partners?city=rome (I can live with this!)

    In my search to perfect my solution, I came across a plug-in called Query Multiple Taxonomies that might be of help (well so it seems by the description of it). But I got no luck with it either (so far).

    Anyway I asked on the plug-in’s forum if it is possible to use that plug-in to do something such as /custompage/taxonomy/term.

    I hope people will participate in the discussion.

    Regards,
    P.

    Thread Starter pmdci

    (@pmdci)

    Found another way of doing this… and I got better results!

    <?php
    global $query_string;
    $paged = get_query_var("paged");
    query_posts("&post_type=partner&orderby=title&order=asc&" . "&paged=" . $paged . $query_string);
    global $more;
    $more = 0;
    ?>

    With this code I can also send url queries to my page template like /partners/?city=rome and it works!!!

    I can’t do /partners/city/rome, but I am really happy with the current results 🙂

    thank you so much!
    I really appreciate your posting the solution you found, as it’s very close to what I am working with.

    Thread Starter pmdci

    (@pmdci)

    Anointed,

    You very welcome. It is refreshing that someone actually manifested himself in this forum, particularly to thank me for my monologue above. I was tired of talking to myself, really 🙂

    If you find a better way of doing it, please let me know. Perhaps there is a way to achieve something like /partners/city/rome ?

    My challenge now is this one: How can I make a widget (a dropdown) that passes the selected value as an addition to the query? Perhaps you have some thoughts on this?

    So far I can do this queries by hand, but not with widgets.

    For instance, say that I browsing my list of partners (in my custom page-partners.php page template). The url for this page is /partners/.

    In this page I want to add another dropdown for users to select cities where they can find partners. By selecting a city from this dropdown in the partners page, it should then return the city as query like /partners/?city=rome.

    So as a result, the user would see the list of all partners listed in the city of Rome.

    Is this possible? What do you think?

    Regards,
    P.

    I have not tried it yet, but Scribu’s query multiple taxonomies plugin comes with a widget that should do exactly what you are asking.

    I plan on installing it tomorrow and giving it a run. It’s 1am here now, so to late to start a project that big. I always mess things up with no sleep lol. If I end up getting it to work, I’ll post back the solution.

    Thread Starter pmdci

    (@pmdci)

    Well it is 05:28 here (hehe) and I just can’t stop 🙂

    If you do try Scribdu’s plug-in, please let me know your thoughts and experiences on Scribdu’s multiple taxonomies plug-in, ok? Last time I tried it, it actually broke what I have done with query_posts() so far. But I think it is important to mention that at that time I was using the old code:

    <?php
    $paged = get_query_var("paged");
    $url = $_SERVER['REQUEST_URI'];
    parse_str($url, $query);
    query_posts("&post_type=partner&orderby=title&order=asc&city=" . $query['city'] . "&paged=" . $paged);
    global $more;
    $more = 0;
    ?>

    … Instead of the new code:

    <?php
    global $query_string;
    $paged = get_query_var("paged");
    query_posts("&post_type=partner&orderby=title&order=asc&" . "&paged=" . $paged . $query_string);
    global $more;
    $more = 0;
    ?>

    There is a parallel discussion at Scribdu’s plug-in forum.

    Cheers,
    P.

    @pmdci
    I appologize about not having gotten back to you about the widget. It’s been a crazy day and I’m still not even home yet. I hope to find time tonight to install the plugin and report back. Just didn’t want to leave you hanging

    Thread Starter pmdci

    (@pmdci)

    Hi anointed,

    Thanks for the feedback. I haven’t tried Scribu’s plugin yet. In fact, he said here that it doesn’t come with a widget to creat such dropdowns… You must add them by yourself…

    Please let know your findings when you have the chance.

    Ahh, I also added the following under after the loops of all my pages that query posts: <?php wp_reset_query(); ?>

    I am not a WordPress GURU, but I think this should be added to templates when making use query_posts(). Anyway adding this didn’t break my work with the URL queries so I added it for now.

    @pmdci
    Well after 3 days of headbanging I got my answer.
    http://core.trac.wordpress.org/ticket/13582

    Unfortunately what we are trying to do at the moment is not supported. Hope it gets added in soon. I’m pretty sure that once 3.0 is mainstream that many people will figure out that you can’t sort like this. Enough requests, and maybe it gets added sooner than later.

    Anyhow, I really did try

    Thread Starter pmdci

    (@pmdci)

    @anointed

    Thank you for the feedback mate!

    Well at the moment I am using /partners/?city=rome or /hotels/?city=rome and I can live with it.

    I do have a question you might help me out with… I started the a new post here about it.

    Cheers,
    P.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Can I pass a query to a custom post type using a custom taxonomy?’ is closed to new replies.