Skip Navigation

[Resolved] Limit search to custom post type posts created by logged-in user

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 5 replies, has 2 voices.

Last updated by Eric 5 years, 4 months ago.

Assigned support staff: Minesh.

Author
Posts
#351816

Hi,

I'm building a membership site. I'm using CRED to allow members to publish posts for a custom post type. I need a search bar that will only search within the custom post type, and (most importantly) only display results for posts created by the logged-in user. Basically, if I'm logged in and do a search, I only want to see my own posts. Other users posts are private and should never be in the search results.

Thanks,
Eric

#351891

I forgot to mention, I'd like the search form to be in the sidebar.
(Trying to figure this out with your docs, but not having much luck)

Update. I've got the search working for the custom post type from the sidebar. I just need to limit the search and results to posts created by the logged in user.

#351922

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Could you please try to use:
[wpv-current-user] and [wpv-post-author] views shortcode to compare values of loggedin user and post author.

For example:

[wpv-conditional if="('[wpv-current-user info='id']' eq '[wpv-post-author format='meta' meta='ID']')"]
your content to display if condition is true
[/wpv-conditional]

The documentation is here:
=> https://toolset.com/documentation/views-shortcodes/#wpv-current-user
=> https://toolset.com/documentation/views-shortcodes/#wpv-post-author
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

#352130

Hi Minesh,

First, thanks for the help...

I already use a conditional for the output, like this:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          [wpv-conditional if="( '[wpv-current-user info='id']' eq '[wpv-post-author format='meta' meta='ID']' )"]
          <a href="[wpv-post-url]">[wpv-post-date]</a>
          [wpv-post-excerpt]
          [/wpv-conditional]
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

What I'm wondering is, do you have a way to pre-filter the search (not the output) to just the logged-in user's posts? For the sake of argument, say the site has 1000 users with 100 posts each, wouldn't it be faster (if I were a user) to only search through my own 100 posts instead of all 100,000 posts? That's the goal.

Plus, even though the conditional hides the other users' posts in the output, those posts still get "counted" as existing in the loop, so the "no-items-found" section doesn't get displayed if my search returns posts (which I can't see) for another user. The search really needs to exclude those posts before they even get to the display loop. Does that make sense?

Thanks,
Eric

#352231

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

screenshot-mineshts.host 2015-12-08 12-24-51.png

If you are willing to display only loggedin user posts for the posts whose author is loggedin user you should add a query filter "Post author" and select option "Post author is the same as the logged in user" and save the filter.

Please find attached screenshot for more reference:

#352469

That worked!

I had to create a view from scratch though, because I'd initially selected the "Display the results as a parametric search" radio button, so the filter section in your screenshot didn't even exist on my view.

Anyway, all good. Thanks for the help!!

Eric