search only logged in user's posts
I’ve searched all about for this but can’t find anything so have created my own thread.
i basically want to create a second search function which is available only to logged in users and which allows them to search only their own posts, but at the same time keeping the first search function (available to all) which searches all the posts. I am using the Sidebar Login plugin and so thought i could paste some code in there (to the ‘user interface’ bit where it lets you add stuff to the sidebar) and it would then only appear to logged in users. At the moment I have inserted:
<form role="search" method="get" id="searchform" action="?php bloginfo('url'); ?>"> <div><label class="screen-reader-text" for="s">Search for:</label> <input type="text" value="" name="s" id="s" /> <input type="submit" id="searchsubmit" value="Search" /> </div> </form>
This gives me the normal search form and then i thought i could just customise it so it only searches the user’s own posts. (Maybe something involving the author=%userid%….?)
Does anyone know if it possible to do that by adapting the above code? And if so how how I can do this??
The key thing is that I want both search forms to exist at the same time
(one which searches all the posts, and the second one which just searches the logged in user’s posts).
PS: the website is http://www.dreamarchive.org/
If you query your page with the author ID as part of the search query string it will filter by author…
You can’t have two forms, with two sets of inputs with the same IDs and names, but the search won’t pass parameters correctly with differing IDs. So what i’d suggest is adding a checkbox to your search form that holds the value of the current user’s ID, when it’s checked, the search will pass this along in the query string(just make sure you give the input an ID and name of author).
NOTE: You can use
author_namein place of
authorif you prefer the query string to contain the username in place of the ID.
Thanks for your reply!
I had already previously tried /?s=search-term&author=1 when i had two forms and it didn’t work. And then i read your post, and (hopefully correctly!) saw that its difficult (impossible?) to have two search forms, and so much easier to get rid of the 2nd and add a checkbox (which adds author id) to the 1st (and now only) search form.
So i got rid of my 2nd search form, but still the /?s=search-term&author=1 ending isn’t working!
On top of this i am struggling to work out what value i need to insert the author’s id automatically through the checkbox. I thought i could just use %userid% but this doesn’t work. i tried playing around with stuff i found through trawling the forums, but i don’t really know what i am doing! Any pointers in the right direction would be great!
/?s=search-term&author=1won’t find anything unless a post has
search-termin the title or content, use something generic to make sure you get matches..
And see if you then get results.. (the checkbox will be of little use if the search requests aren’t working with the author parameters).
(i wasn’t actually searching for “search-term” but rather replaced it with a search term.)
nothing generic seems to apply the author filter… for example i try
to search for all posts by the author with id=1 which include the word “horse”. and i get simply all the posts that include the word “horse”, without the author filter being applied.
i tried also with author names and still nothing.
is there obvious i am missing? something i haven’t done?
PS: just so you know, in case it makes any difference, i am using the journalist v1.3 theme which doesn’t have a searchform.php (or search.php) and so i am editing the search form in the general-template.php found in the wp-includes folder.
There’s no need to be making changes to core WordPress files, the best approach would be to copy one of your theme’s template files and naming it search.php(choose which ever suits how you want search pages to appear, archive.php may be an obvious choice, but choose at your leisrue), so your theme has a search template, it may even fix your problem with not being able to request searches by author. I have an idea why the requests don’t work, but you should be able to resolve that by creating a search template for your theme..
hi. i have put now a search.php template in my theme. (i just simply copied then renamed page.php)
but still the author filter doesn’t work…
any ideas about why?
is there something i am going to have to do involving php code?
Might i suggest switching to the default theme, just for a couple of minutes and seeing if searches (with and without author parameters) are working, if they do your problem is specific to how the theme is handling search requests..(be sure to test with plugins disabled to).
Is this the theme you’re using?
Hi, no i am using The Journalist version 1.3 (this one http://wpthemes1.wordpress.com/category/the-journalist-v13/).
OK so i went through disabling each plugin and figured out what the problem is.. “WPSearch” the plugin which (is meant to!) enhance your searching. It works really well in some respects.. for example if you search for “war” it doesn’t just pick out any word which involves the letters “w-a-r” togehter, as the simple search does. you also get more control over what kind of content it searches etc.. BUT obviously there is some kind of problem with the author filtering.
i’ve looked on the web but can’t find any report of this problem
do you think i should just try out some other search plug ins?
or can you suggest a little fix i can do to it ?… (because otherwise i am very pleased with how it performs)
So the search is working correctly with the search plugin is disabled? If that’s the case i’d suggest creating a support topic specific to your problems with the plugin(the plugin author can better advise on solutions to problems with his/her code).
- The topic ‘search only logged in user's posts’ is closed to new replies.