• Hi All,

    i’m looking for two pieces of code that can help me with the following:

    I got posts and pages with a custom menu. These posts/pages got a custom template activated. The templates contains the menu for the specific posts and pages. This way i can order pages/posts in two different categories.

    Now i like to change the search function; the search output should be based on the active custom menu category.

    I named menu 1: Benelux and menu 2: International

    1: First peace of code needs to be for the searchform.php
    The code should look at the current active menu(category) and the search result should be based on the active menu.

    If the menu is for example “Benelux” then the search result should only display pages/posts from the custom taxonomy that i made <input type=”hidden” name=”location” value=”benelux” />
    Or if the active menu is “International” then display pages/posts from the custom taxonomy <input type=”hidden” name=”location” value=”international” />

    I found the following code to get the menu category…not sure if this is useful..

    (wp_nav_menu( array(
        'menu' => 'benelux'
    ) )

    2: Second peace of code is needed for the search.php (header).
    Because if the output should work correctly than we also need to change the menu for this search result page. Default the search.php uses the main menu.

    So the following code needs to see if the pages/post contain taxonomy benelux or international and change the menu accordingly.

    It would just be really awesome if this is possible.

    Greatings,
    Taro

Viewing 15 replies - 136 through 150 (of 152 total)
  • Thread Starter Taro

    (@sicktb)

    i have put it like this:

    <?php
    global $region;
    echo "Region: $region<br>\n";
    echo "Referrer: {$_SERVER['HTTP_REFERER']}<br>\n";
    echo "Request: {$_SERVER['REQUEST_URI']}<br>\n";
    ?>

    Moderator bcworkz

    (@bcworkz)

    Thanks for fixing that, You’ve probably come to expect imperfect code from me by now :/

    I’m rather mystified about what’s the root cause now. Let’s try forcing the Benelux region no matter what’s in the URL. In the tg_set_region() function declaration, change this line:
    $region = $explode[1];

    to this:
    $region = 'benelux'; //$explode[1];

    A slightly related issue is I noticed the search form doesn’t have the hidden region field in it, so search results are not constrained by region. See if you can determine why and correct it. I forget the exact code we used, but either the entire code for the field got dropped, the if conditional is not properly satisfied, and/or the global $region; is missing.

    Thread Starter Taro

    (@sicktb)

    the hidden field is active again in the searchform.php:

    <?php
    echo "<input type='hidden' name='category_name' value='$region' />";
    ?>

    I changed the code in function.php.

    For the info; the wp-login.php will not redirect to the front/home page after login.

    Moderator bcworkz

    (@bcworkz)

    I think redirecting the wp-login.php to the front page will not be difficult, but to avoid confusing things, I’d like to focus on fixing the worldwide continuing to show up in the search URL. This is the most mysterious problem.

    You’ve reported earlier that removing my code from functions.php fixes the problem, but all my efforts to isolate which part of my code is causing this have failed. Let’s try to at least identify which filter or action hook is the problem. Please comment out every line in my code that begins with add_action or add_filter. This will be equivalent to removing all related blocks of code. Only the navigation function will still be active, and I cannot even imagine how that could influence the worldwide redirect.

    Once all specified lines have been commented out, try doing a search from the sample page. The worldwide redirect should no longer occur and the search results page URL should be domain.com/benelux/region-search followed by several URL parameters. If it’s still worldwide, I don’t see how my code could be the cause.

    Assuming the worldwide has gone away after commenting out those lines, uncomment the first add_* line and check if the worldwide thing has reappeared or not. If not, uncomment the next add_* line and test again. Continue in the same manner for every commented out add_* line. Please report back to me which line was last uncommented when the worldwide reappeared.

    Something else to look at later and probably easy to fix is the actual search term is apparently not making it into the search query arguments. It appears all the posts returned are at least Benelux, but the search term is ignored. I don’t think the search term keyed as “term” in the URL is what the query arguments on the region-search template are expecting. If you can identify the problem with that and fix it, fine, otherwise I’ll look at it later after this worldwide thing is resolved.

    Moderator bcworkz

    (@bcworkz)

    FYI, the forums admins are migrating the forums to a newer version of bbPress and all older threads will be closed in preparation. Since this thread started so long ago, it will be closed. You’re welcome to open a new topic in the Hacks sub-forum to continue our conversation.

    Thread Starter Taro

    (@sicktb)

    crap i have to back-up this forum posts…

    Moderator bcworkz

    (@bcworkz)

    This thread should remain available for reading AFAIK, you just will not be able to add new posts to it. It’s not supposed to happen until Friday sometime. If the forums are not working around then, it’s probably because of the migration. It may not be a bad idea to grab anything useful here just in case, but it shouldn’t be necessary.

    Thread Starter Taro

    (@sicktb)

    // add region category to main queries
     add_action('pre_get_posts', 'tg_set_region');
    function tg_set_region( $query ) {
      if ( get_option('page_on_front') == $query->get('page_id')) return;
      if ( !is_admin() && $query->is_main_query()) {
        global $region;
        $explode = explode('/', $_SERVER['REQUEST_URI']);
        $cat = $query->get('category_name');
        $region = 'benelux'; //$explode[1];
        if ( '' != $cat ) $cat .= '+' . $region;
            else $cat = $region;
        $query->set('category_name', $cat);
      }
    }

    When i remove the add action here the region will be removed. I tryed all the options but im not getting only benelux.

    Region:
    Referrer: https://link.com/benelux/sample
    Request: /region-search?term=basel&category_name=&submit=Search

    Thread Starter Taro

    (@sicktb)

    i think it has to do with “$region = ‘benelux’; //$explode[1];”

    Thread Starter Taro

    (@sicktb)

    The menu for worldwide was the one from benelux atm
    i have put back $region = $explode[1]; to fix this.

    If you look at the breadcrums; the link “Intro” should be the startpage. This is changed in the function code at the moment. When i remove all the added function code the “intro” link/page is working again.

    Moderator bcworkz

    (@bcworkz)

    We better stick to one problem at a time. Back with benelux/region-search requests being rewritten to worldwide. With the add action to ‘pre_get_posts’ commented out, if you try to go to domain.com/benelux/region-search/, does the URL region rewrite to worldwide still, or does it now stay as benelux, or is there no region portion at all? From your post you said you saw “Request: /region-search?term=basel&category_name=&submit=Search”, so it appears there is no region at all, correct? If that’s the case, my action hook to ‘pre_get_posts’ cannot work properly, the entire premise was that the region would be available in the request. It’s apparently getting stripped out by something and I think it is unrelated to the code I had you add.

    Any idea why the region no longer appears in the request? This part of your site I never understood, I just trusted it to work, and now it apparently doesn’t ??

    Thread Starter Taro

    (@sicktb)

    Any idea why the region no longer appears in the request? This part of your site I never understood, I just trusted it to work, and now it apparently doesn’t ??

    Let me try to explain it:

    First thing i did is i made 2 pages one i called benelux and the other worldwide. Alle the subpages i have linked to the correct region page.

    Also i made a custom region taxonomy called “Location”. Here i can select if a page or post is “benelux” or “worldwide”

    see here my currect function.php

    Thread Starter Taro

    (@sicktb)

    I found the problem; and im sorry the region search page was not linked as subpage to /benelux/

    Thread Starter Taro

    (@sicktb)

    ok we are back! domain.com/benelux/sample works correct.

    The pre_get_postst and $explode[1]; is active again.

    List to do:
    tags, blog, archives, category etc
    pagination css?
    more?

    Moderator bcworkz

    (@bcworkz)

    Whew! I’m glad you worked that out, it was blowing my mind since I’m not able to have unfettered access to your site. While you may be willing to grant that to me, it’s outside the scope of these forums, I need to work blind to some extent. You’re my eyes ??

    The biggest problem I see now (I’m not totally blind ?? ) is the single page links (“Read More”) get caught in a circular redirect, ending in “this page isn’t redirecting properly”. See if commenting out add_action('init', 'tg_home_by_region'); fixes the problem or not. If not, comment out my other hooks as before to see if it’s my code or somewhere else.

    FYI, the forum migration has been pushed back from Friday to next week some time. When this topic gets closed, I’m going to let you start a new topic even if it’s my “turn” to post. Don’t only rely on getting an email notification, if you don’t hear back from me in a reasonable time, check to see if this topic has been closed.

Viewing 15 replies - 136 through 150 (of 152 total)
  • The topic ‘Search result based on menu category’ is closed to new replies.