• Resolved amasuriel

    (@amasuriel)


    I am new to wordpress development and am getting very frustrated with this.

    First, what I am trying to accomplish is this: I have a page template (and a published page) that has some form data (some dropdowns and a text field). I want to take these field values and query wp_usermeta to get a table with cetain meta_key / meta_value pairs…I am not using the info to get posts.

    First I had a terrible time figuring out how to pass parameters to the results page at all, since having a submit button appends the Submit x and y values etc to the post.

    Eventually after some trial and error and a lot of searching I found this: /www.rlmseo.com/blog

    I did this, and now I’m closer but not there yet.

    See adding a filter like add_filter('query_vars', 'add_query_vars'); in my functions.php (in my theme) did not add the variables returned in the add_query_vars method to query_vars, at least by the time it got to my templated pages.

    Adding add_filter('request', 'add_query_vars');got it to show up sortof in the template…but instead of getting a query_var with a key= to my vars in add_query_vars with a value of the rewritten querystring parameter (if it existed) I get a key=0, 1 etc with a value equal to the vars returned in add_query_vars.

    I hope that makes sense. I can post additional code if that would be helpful. Any insights would be great…I have spent way to long trying to get a simple search working.

Viewing 15 replies - 16 through 30 (of 39 total)
  • Thread Starter amasuriel

    (@amasuriel)

    Yes.

    More importantly redirected to blogurl/advertisers without my parameters in query_vars.

    To reproduce just make whatever test /advertisers page you set up from before a Page and try to process the query_vars in the page template.

    If you attach a debugger and set a breakpoint in wp-blog-header.php you will see it gets processed twice; once the query_vars are set (after it does the rewrite from blogurl/advertisers/province/city/service), the second time they wont be.

    Edit: I mean it gets processed twice when you request blogurl/advertisers/province/city/service once

    Ok i can go test it, i still have the page from before…

    What specifically do you want me to test for in the page template? Do you want to print out something in particular or test for a particular var, just so i know what i’m aiming for…

    ??

    Thread Starter amasuriel

    (@amasuriel)

    Just that you can access the added query_vars (like province) from the template.

    You’re gonna hate me for this, but i can still query ..
    wordpress.localhost/advertisers/a/b/c/

    ..when using a page template on the page..

    I added something simple in the template..

    <?php
    /*
    Template Name: test
    Description: blah
    */
    get_header();
    
    echo 'hello';
    
    get_footer();

    Both..

    wordpress.localhost/advertisers/
    and
    wordpress.localhost/advertisers/a/b/c/

    Work just the same and show the same content, obviously, but point being i’m not getting redirected.

    Thread Starter amasuriel

    (@amasuriel)

    Sorry I had a meeting.

    Yes you will still get the page, but will the below work for you where /a/ is added to query_vars as province?

    <?php
    /*
    Template Name: test
    Description: blah
    */
    get_header();
    
    global wp_query;
    echo wp_query->query_vars['province'];
    
    get_footer();

    Also is the final URL you see when the page comes up after browsing to https://blogurl/advertisers/a/b/c = https://blogurl/advertisers/a/b/c or https://blogurl/advertisers?

    echo $wp_query->query_vars['province'];

    Outputs a if the URL is.

    url/advertisers/a/b/c/

    If the URL is..

    url/advertisers/

    Then obviously the query var isn’t there, per what i was saying before regarding query vars being requested in order to be in the query vars array.

    Final url is url/advertisers/a/b/c/ if that’s what i query for, i’m not getting redirected if that’s what you mean..

    Thread Starter amasuriel

    (@amasuriel)

    Ah so maybe this is the issue then.

    If I request url/advertisers/a/b/c/
    in the end I get url/advertisers…which given that that permalink for the page is url/advertisers makes sense does it not?

    How does it know what the permalink is from url/advertisers/a/b/c/?

    Edit: my permalink structure is set to /%category%/%postname%.

    I assumed you were performing some form of search query that resulted in a request for the advertisers page with the custom query vars.

    Is that where you’re having problems? ..

    Thread Starter amasuriel

    (@amasuriel)

    Yes.

    So the permalink for this page is /advertisers.

    /advertisers needs to be passed parameters so it can build a table of results; which I understood was done doing rewrite rules and a format like /advertisers/param1/param2/param3

    but when I request /advertisers/param1/param2/param3 the final URL I see is /advertisers. This makes sense to me since the rewrite rules we are both using (you are still using the rewrite rules in the test right?) convert /advertisers/param1/param2/param3 to /advertisers&paramname=param1 etc internally…how can you be getting /advertisers/param1/param2/param3 as the final URL if the rewrite rules are enabled?

    Thanks again for all your help, I know this is taking a lot of your time.

    If i understand correctly i believe you will ultimately need a redirect to forward the search query on..

    Right now all we’ve done is allow for requests in the format of..

    /advertisers/province/city/serviceField

    ..to be mapped to index.php?pagename=advertisers&province=example&city=example&serviceField

    However, when you make a search query you’ll find your URL is formed in a standard query string manner (if you set the form method to get)..

    ?province=example&city=example&serviceField=example

    …and that’s side stepping the issue of including the page in the query string to..

    Ok, so moving on from the query string, what you need to do, to get the above query string into the pretty format, ie. /province/city/serviceField is setup a redirect for those kind of requests .. that requires a secondary rewrite rule in itself …

    In order to take any query string(and i’m speaking generally here), like ?one=hello&two=world and turn it into a pretty URL, two rules need to be setup, one that redirects those requests to the pretty equivalent, in this case /hello/world/ and another rule that maps those requests to a location to interpret them … eg. example.com/index.php?one=hello&two=world (this part we’ve done)..

    So i think the question remaining is where and/or how should we catch those requests and perform a redirect to the pretty URLs which are now in place..

    I’ve done something similar before.. so it’s just a matter of setting up some redirects to assist the rules created earlier.. (i can help you do that no problem).

    Question though, where are you setting up the search form that’s going to be performing the requests, in the advertisers page?

    Thread Starter amasuriel

    (@amasuriel)

    Okay, so the part I’m still missing then is some additional redirection.

    If you can help me with that I would appreciate it; I hope you like beer because I owe you a lot of it ??

    /advertisers is both the form and the results; it has some inputs for criteria an a PHP include before the footer that will take the paramaters (once I manage to pass them) and build a results table after some DB queries.

    The search button on the form will just redirect to the /advertisers page again with the optional parameters added to the URL on the fly with some javascript.

    The search button on the form will just redirect to the /advertisers page again with the optional parameters added to the URL on the fly with some javascript.

    So do you have a working search form as yet, even if for now it’s sending requests like..

    /advertisers/?province=example&city=example&serviceField=example

    If you’ve got that part done already(that’s down to the search form) then there’s only the redirection to do..

    Your form should be making requests in the above shown format, once it’s doing that we can work on setting up redirects for those requests..

    Thread Starter amasuriel

    (@amasuriel)

    Ah but if I send /advertisers/?province=example&city=example&serviceField=example I get a 404 error.

    Or we still need to do one more redirect to make that work?

    Edit: The URL style is trivial to change, so consider it done already.

    You shouldn’t do, it works for me.. (with the original page and the initial template added earlier on)..

    It’s not about a redirect or particualr style, the URL should work as is, and that’s the format your search should be making requests in, a redirect is going to mean little if that doesn’t work, because that’s essentially the URL the pretty one is mapped to..

    /advertisers/example/

    would resolve to..

    /advertisers/?province=example

    If the second one isn’t working, it’s not going to matter that the URL looks pretty, do you see what i mean?

    Thread Starter amasuriel

    (@amasuriel)

    I understand what you mean. I’m not sure what is causing /advertisers/?province=example to 404…I had assumed that was standard wordpress behavior…like I said, I’m new on the platform ??

    So if there is something funky with the redirects causing it to 404 when it shouldn’t maybe I can take another approach.

    I discovered reading around today that I can make a normal php page “wordpress aware” by adding <?php include(‘wp-blog-header.php’); ?>, so maybe I should just make a straight php page that just spits out the results I want and I can use AJAX to make the call and populate the results inside the /advertisers page.

    Will that work? There isn’t anything weird with Ajax requests or calling a php page directly with something like searchservices.php?province=British+Columbia this in WordPress is there?

Viewing 15 replies - 16 through 30 (of 39 total)
  • The topic ‘Problems adding to query_vars’ is closed to new replies.