• Hi guys,

    I am need of some help. I have built a client dashboard site. it requires that clients can search for a list of their meetings. I have a search function already in use on the site that brings up only the results from 1 cateogry however I need it to pull up results from a category based on the clients username.

    I have it setup so that all the clients meetings go into a category called – ‘username’-meetings

    I am using the function get_posts('category_name='.$username.'-meetings');

    to pull in a list of meetings and it works fine. How can I incorporate that into this

    <div id="clientSearch">
    		<form method="get" id="search" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" value="enter keywords here" name="s" id="search_input" />
    <input type="hidden" name="cat" value="8" />
    <input type="submit" id="searchsubmit" value="Search" />
    </form>
    </div>

    I figure it has to go into the value but can’t workout how to do it

    thanks in advance
    Matt

Viewing 5 replies - 1 through 5 (of 5 total)
  • If i assume i’m following you correctly, i think this is what you’re after..

    <?php $cat_to_search = get_cat_ID( $username.'-meetings' ); ?>
    <input type="hidden" name="cat" value="<?php echo $cat_to_search; ?>" />

    get_cat_ID() returns 0 on fail, so you’ll just pass in 0(any category) if the call fails..

    Hope that helps..

    Thread Starter graphiccontent

    (@graphiccontent)

    Hi Mark,

    Thanks for that, I added it but it still pulls in all posts from everywhere with the keyword used.

    Perhaps I have done it wrong…..I added this –

    <div id="clientSearch">
    		<form method="get" id="search" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" value="enter keywords here" name="s" id="search_input" />
    <?php $cat_to_search = get_cat_ID( $username.'-meetings' ); ?>
    <input type="hidden" name="cat" value="<?php echo $cat_to_search; ?>" />
    <input type="submit" id="searchsubmit" value="Search" />
    </form>
    </div>
    <span id="indicator" style="display:none;">Please wait...</span>
    <div id="searchresults" style="display:none;"></div>

    Sorry if this is really dumb, I am not the most php literate person on the planet ??

    if you could show me how to combine it into the search form that would be great

    Thanks again
    Matt

    I assume you are setting the $username variable, is that the case?

    Also, does the form point at itself for a particular reason, it ideally should point at the index.. so in turn your search query(when you hit submit/search) looks something like this..

    https://example.com/?s=searchterm&cat=N

    The form action determines where the request is sent, $_SERVER['PHP_SELF'] will vary depending on the given page being viewed..

    Is this your standard WP search or are you trying to add the search on a particular page?

    Thread Starter graphiccontent

    (@graphiccontent)

    Hi Mark,

    The search is embedded in the page and uses JS to display the results in the hidden div under the search box on submit. As I mentioned this is working for when I use the value=’cat id’ but just can’t get this to work dynamically with the username.

    I really am a little stuck with this. Anymore help you could give or an example of the code you would use would be much appreciated and I’d be more than happy to donate for a coffee and donut for ya if we can get this working!

    Thanks
    Matt

    Are you setting the $username variable? It’s not in the code you posted..

    NOTE: get_cat_ID expects the category “name” and not the “slug”, the way you’re forming the category name appears to look like a slug, could that be the issue?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Search Category based on username’ is closed to new replies.