Skip Navigation

[Closed] Filter Results from View (based on shortcode/query)

This support ticket is created 2 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 2 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#2269465

Tell us what you are trying to do?
I would like to allow users to submit/register for a specific event or challenge using forms. They should only be able to register ONCE. I have added the form and manage to display the results of a view that includes all the events/challenges. However, these should be filtered to exclude challenges that have passed (post_meta field (end_date) before today AND participants should only be able to register for challenges which they have not registered for before (events already registered for should not be displayed).

Is there any documentation that you are following?

Forums and other resources provided.

Is there a similar example that we can see?

Access can be provided where necessary.

What is the link to your site?

hidden link.

#2270223

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

"participants should only be able to register for challenges which they have not registered for before"

How do users register for events? How would the View know if someone has registered for an event already?

#2270317

Hi Nigel,

Users would register using a Toolset Form which would then connect their user_id to the challenge_id.

I would need to check whether the user_id is already connected to the challenge and if so, not to display challenge. This is obviously easy to do with a standard query but not sure how to implement this in views.

Hope this helps?

#2270397

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

"which would then connect their user_id to the challenge_id"

That is the key bit of information, how do you do that?

You store the challenge_id as a custom field of the user? Or you store the user_id as a custom field of the challenge post?

Either way sounds like a one-to-one connection, but presumably more than one user can sign up to a challenge (and can a user sign up to more than one challenge?).

#2270467

Hi Nigel,

Submitting the form generates a post of type 'entry' connected to the user_is/post_author which in turn has some meta fields connected (one of these is the challenge_registration_id) which refers to the challenge registered for.

Users can register for any number of challenges and any number of users can register for a particular challenge.

Hope this makes sense?

#2270499

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, so you have something like a profile post linked to the user (entry) and then you link that post to the challenge post.

It sounds like the way you are connecting them (the entries and challenges) is to effectively manually maintain a kind of relationship between them.

I suspect you would be better served by using Toolset Relationships, with a many-to-many relationship between entries and challenges, in which case you would be able to add a relationship query filter to your View.

To learn more about that see https://toolset.com/related-lesson/post-relationships/

If you want to persist with your current implementation, you will likely need some custom code to implement what you are looking for.

"This is obviously easy to do with a standard query but not sure how to implement this in views."

Regarding the last part, the query part of Views is basically just a visual UI for passing WP_Query arguments, i.e. it is built on top of the core WordPress query class. So most things you can do with a "standard query" you can do with Views.

Where you can't you can use the Views API (specifically the wpv_filter_query filter: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query) to modify the query arguments as required before the query is created.

Your problem is, I think, more fundamental.

When you query something you can filter the query by properties of the thing being queried.

So if you are filtering challenge posts you can filter posts by properties of challenge posts, including its custom field values.

But you want to filter by something which is a property of something other than that which is being filtered, i.e. the challenge_id field stored on entry posts.

(It would be like saying "show me Premier League football clubs with a population greater than 1 million". Football clubs don't have populations, although they are associated with towns and cities, which do.)

One solution is to run the query in two parts. Create a View for challenge posts that just has the date query.

Then use the wpv_filter_query hook, and run your own query (via get_posts) to get any entries for the current user, and extract the challenge_id values from those posts into an array, and then update the View query arguments with an entry for 'post__not_in' where you pass it that array. So any challenge posts that the current user has already created an entry for will be excluded from the results.

The topic ‘[Closed] Filter Results from View (based on shortcode/query)’ is closed to new replies.