Filter by current post?
-
Hello,
Can we filter the export file based on current embedded post and current author?
I noticed this is possible using filter
But I’m not sure how to do it?
Can you guide me?
Thank you so much
-
Hi @razor89,
I’m not completely sure what you mean. Do you want to filter the results of the normal output (so the public download url without any filters attached) based on the current logged in user, where that user is also the creator of the entry?
And what do you mean by embedded post. It’s probably possible; but I would like a full example to create a hook you could use.
Thanks.
I used one form for all custom post that created by users.
If I don’t use filter, all data from all custom post from different author will be appear in the download file.
So I just want if user download the file, the data will be based on their own post.
Btw one user just can create 1 custom post.
hopefully you know what I mean.
Thank you so much for the help.
@razor89 so does that mean the results will only include one result, because the user can have only one post?
Or are there separate entries based on a post that is connected to the user?
Can you perhaps send me an export of your form (as .json) and an example file of what you would like it to include?
Please send it to wordpress@doeken.org
Thanks!
@razor89 I figured I’ll at least send you this hook. This will filter the results on the current logged in user. If you can get the post id you need, that you can also filter on that post id. Just remove whichever filter you do not want.
Hope this helps.
/** * Hook that filters gravity export results on the current user and post id. */ add_filter( 'gfexcel_output_search_criteria', function ( array $search_criteria ) { // filter entries created by current user. $search_criteria['field_filters'][] = [ 'key' => 'created_by', 'value' => get_current_user_id(), ]; $post_id = 1; // find a way to fill this variable // filter entries on post id $search_criteria['field_filters'][] = [ 'key' => 'post_id', 'value' => $post_id, ]; return $search_criteria; } );
- This reply was modified 2 years, 10 months ago by Doeke Norg. Reason: fix indenting
Sorry for late reply, thank you so much @doekenorg
I tried the code, but looks like the result just the current user who fill the form.
Maybe if nice if the result will be based on source url of the form.
Thank you
or permalink it’s okay too
Hi @razor89 ,
Sorry, but I’m having trouble understanding your question.
You said filter on the current author; maybe you are talking about the other of a post? I’m very confused.
1. What does your form do? Does it create posts?
2. Do you have one form on multiple pages? And do you need the entries posted from that page?
3. Can you give me step by step how a post is filled, what it contains, and what you would like as the result? Please leave no detail out.
4. Please provide your form as a .json towordpress@doeken.org
. This will probably help in clearing this up.Thanks, and again sorry for my confusion.
Hello Doeke,
sorry for late respond.1. What does your form do? Does it create posts?
No, just to create the comment.
so the form just use to create comment (to replace the default comment in WordPress)
2. Do you have one form on multiple pages? And do you need the entries posted from that page?
yes one form for multiple pages, just need to filter the entries based on each post and author of current post can download it.
3. Can you give me step by step how a post is filled, what it contains, and what you would like as the result? Please leave no detail out.
I’m using acf to create the post (one post per user). The result will be all entries based on current author of the post4. for sure will send it now
Btw thank you so much for your support, really appreciate it.
Hi @razor89,
Thank you for providing this context. This helped a lot. I think this is what you need:
I used a form with the id
51
. Please update the filter with the correct form_id for your form. Since you provided me with your form, the field id (4
) should be correct.This code will retrieve all page id’s the current user has made. Then it will filter all results to only include any entries that were posted on pages that have one of those ID’s. This makes it so you can use this for multiple pages for the same person too, but also just for a single page.
Hope this helps you out.
/** * Hook that filters gravity export results for the current user. */ add_filter( 'gfexcel_output_search_criteria_51', function ( array $search_criteria ) { $ids = get_posts( [ 'author' => get_current_user_id(), 'fields' => 'ids', // We're only interested in the id's of these pages / posts 'status' => 'publish', // only check for published pages 'post_type' => 'page', // if it is a post (and not a page) you can remove this line 'numberposts' => - 1, // all results ] ); // filter entries on post id $search_criteria['field_filters'][] = [ 'key' => 4, 'operator' => 'IN', 'value' => $ids, ]; return $search_criteria; } );
I’m sorry Doeke
It didn’t work, I tried in several past days but still not work.
But thank you so much for your help
@razor89 can you provide an environment that doesn’t work I can fix? Or can we use a chat service to have a quick chat to fix this?
Please let me know on wordpress@doeken.org
Hi @doekenorg
all is good now and the data filtered based on post id now π
Thank you so much, and I really appreciate it for your help
Btw I changed the code with this below:
`/**
* Hook that filters gravity export results for the current user.
*/
add_filter( ‘gfexcel_output_search_criteria’, function ( array $search_criteria ) {
$ids = get_posts( [
‘author’ => get_current_user_id(),
‘fields’ => ‘ids’, // We’re only interested in the id’s of these pages / posts
‘status’ => ‘publish’, // only check for published pages
‘post_type’ => ‘custom_post’, //I change this with my custom post type
‘numberposts’ => 1, // all results
] );// filter entries on post id
$search_criteria[‘field_filters’][] = [
‘key’ => 4,
‘operator’ => ‘IN’,
‘value’ => $ids,
];return $search_criteria;
} );@razor89 thanks for letting me know. Glad it worked out!
- The topic ‘Filter by current post?’ is closed to new replies.