Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Display posts based on custom field
This topic contains 8 replies, has 2 voices, and was last updated by kokosan 2 months ago.
- AuthorPosts
- January 30, 2023 at 10:52 am #41149
Hi, I have another search instance. The source is post type and
1) I have a custom field text called title_field for example for the posts but not all the posts have the value of this custom fied, for example:
Post1
Post2 (this post has the custom fied value)
Post3
Post4 (this post has the custom field value)
Post52) And in the Advanced Options, I will show this custom filed text using {title_field} and this custom field contains a url text that will redirect to my custom location. So, In this case I won’t use the default title. My question is how can I display only the posts that have this custom field like this:
Post2
Post4Now It displays all posts including the posts that have empty custom field value as explained in the number 1)
3) But in the filter, I will still use the default post categories and post tags to filter the posts as normal and in this case I won’t use the custom filed in the filter but just only want to display the posts based on this custom field as mentioned in the number 2)
In conclusion, I don’t want to create another custom post type to achieve this because I still want to use the post categories and post tags of the post type to filter posts, so just want to use the custom field to replace the default post title. And in this case, it shouldn’t affect my another search instance that use the default post title.
January 30, 2023 at 5:08 pm #41154Hi,
Unfortunately the only way of filtering this is programmatically, by removing results after the search is finished, by checking for the “title_field” existence.
Something like this:
Best,add_filter('asp_results', 'asp_remove_where_no_title_field'); function asp_remove_where_no_title_field($results) { foreach($results as $k=>&$r){ if ( get_post_meta($r->id, 'title_field', true) == '' ) { unset($results[$k]); } } return $results; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 30, 2023 at 7:01 pm #41155I’ve tried the code, ok,it removes the title that doesn’t contain the custom field. However, it doesn’t display properly and having an issue with load more (globally). I thnk I will use custom post type to acheve this. Thank you.
January 31, 2023 at 10:29 am #41162That is the proper way to resolve this. A custom post type will make this so much simpler, in the future if you want to do other stuff (like custom queries, displaying items in a page builder etc..) it will work seamlessly.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 31, 2023 at 11:15 am #41164I have 2 search concepts. One search concept still uses default title while the other doesn’t, but use custom field. The code applies globally to both search concepts. So, it’s not possible to apply the code to a specific search instance (the one that uses custom field) without affecting the other (the one that uses default title)?
January 31, 2023 at 11:19 am #41165Sure, it is possible by adding an additional argument and a tiny modification:
add_filter('asp_results', 'asp_remove_where_no_title_field', 10, 2); function asp_remove_where_no_title_field($results, $search_id) { if ( $search_id == 1 ) { foreach($results as $k=>&$r){ if ( get_post_meta($r->id, 'title_field', true) == '' ) { unset($results[$k]); } } } return $results; }
On line
Best,if ( $search_id == ...)
change the number to the search ID you want to apply the code to.
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 31, 2023 at 12:09 pm #41166Ok, now I can apply the code to the specific search instance, thank you.
1) I add a the custom field value to one post that has category Book 1 and one post to catgegory Book2. But it show up only the post that’s in category Book1 and load more show nothing. But when I unckeck ctaegory Book 1 in the filter, the post that’s in category Book2 then shows up.
2) Another thing is Load more button that shows many counts and when I click it show nothing. How to load only posts that have this custom field value, the post category, and the post tag only? Load 10 posts per time.
And here is a video to reproduce the issue https://drive.google.com/file/d/1Ywl8mboG0PeX2s_zK7kYdXEuekqo_hZq/view?usp=sharing
January 31, 2023 at 1:41 pm #41168This is all due to this custom code, because it alters the results set after the search is completed. Unfortunately there is no way to get around these issues. It is always better to change the query by adding arguments, but in this case it is not possible, only by removing the elements after the search. That will affect the search negatively. That is why it is much better to use a custom post type instead.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 31, 2023 at 3:20 pm #41170Ok, I will go with cstom post type then.
- AuthorPosts
You must be logged in to reply to this topic.