Display posts based on custom field

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 kokosan 1 year ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #41149
    kokosan
    kokosan
    Participant

    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)
    Post5

    2) 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
    Post4

    Now 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.

    #41154
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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:

    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #41155
    kokosan
    kokosan
    Participant

    I’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.

    #41162
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    That 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 :)


    #41164
    kokosan
    kokosan
    Participant

    I 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)?

    #41165
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Sure, 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 if ( $search_id == ...) change the number to the search ID you want to apply the code to.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #41166
    kokosan
    kokosan
    Participant

    Ok, 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

    #41168
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    This 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 :)


    #41170
    kokosan
    kokosan
    Participant

    Ok, I will go with cstom post type then.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.