Ignore unlinked Attachments

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Ignore unlinked Attachments

This topic contains 11 replies, has 2 voices, and was last updated by AdditiveFX AdditiveFX 4 years, 8 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #21401
    AdditiveFX
    AdditiveFX
    Participant

    I wish to search for attachments that are attached to some page or post. I have implemented this in PHP by filtering the results but I can not do that in the AJAX results.

    Ideally this would be an option in the highlighted part of my screenshot (“Link results to – attachment parent post – and if parent does not exist then – IGNORE RESULT”) or something like this.

    Please get in touch with me abou this.

    Attachments:
    You must be logged in to view attached files.
    #21403
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    While there is no feature/option for that on the back-end, it should be possible by using a custom code. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    add_filter('asp_results', 'asp_results_remove_unlinked_attachments');
    function asp_results_remove_unlinked_attachments($results) {
        foreach ($results as $k => &$r) {
            if ( $r->content_type == 'attachment' ) {
                $parent_id = wp_get_post_parent_id( $r->id );
                if ( is_wp_error($parent_id) || empty($parent_id) ) {
                    // Remove the post from the results list
                    unset($results[$k]);
                }
            }
        }
        return $results;
    }

    I hope this helps!

    Best,
    Ernest Marcinko

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


    #21404
    AdditiveFX
    AdditiveFX
    Participant

    Hey, thanks for the speedy reply 🙂

    I have implemented this and it doesn’t seem to work :/ Is this supposed to also filter the AJAX results?
    Right now there is no difference in the results, do I need to empty some sort of cache in your plugin for the changes to take effect?

    Thanks!

    #21405
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Yep, that should filter the live results. I have made a correction to the code right after I posted, you might still got the original post. Can you please verify, that this is the code:

    add_filter('asp_results', 'asp_results_remove_unlinked_attachments');
    function asp_results_remove_unlinked_attachments($results) {
        foreach ($results as $k => &$r) {
            if ( $r->content_type == 'attachment' ) {
                $parent_id = wp_get_post_parent_id( $r->id );
                if ( is_wp_error($parent_id) || empty($parent_id) ) {
                    // Remove the post from the results list
                    unset($results[$k]);
                }
            }
        }
        return $results;
    }

    You don’t have to clear the cache, this should work effective immediately.

    Best,
    Ernest Marcinko

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


    #21512
    AdditiveFX
    AdditiveFX
    Participant

    Hi,
    I have tried this and no luck.

    I think I know why it is not working: when I return $r->content_type somewhere visible it’s always “pagepost” – never “attachment”

    Is there something wrong in my settings?

    add_filter('asp_results', 'asp_results_remove_unlinked_attachments');
    function asp_results_remove_unlinked_attachments($results) {
        foreach ($results as $k=>&$r) {
           $results[$k]->title = $r->content_type;
        }
    	return $results;	
    }

    this always returns “pagepost” as the search item title.

    #21513
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    I think you might have an older version of the plugin, I am not sure. In that case, this modification should do the trick:

    add_filter('asp_results', 'asp_results_remove_unlinked_attachments');
    function asp_results_remove_unlinked_attachments($results) {
        foreach ($results as $k => &$r) {
            if ( isset($r->post_type) && $r->post_type == 'attachment' ) {
                $parent_id = wp_get_post_parent_id( $r->id );
                if ( is_wp_error($parent_id) || empty($parent_id) ) {
                    // Remove the post from the results list
                    unset($results[$k]);
                }
            }
        }
        return $results;
    }
    Best,
    Ernest Marcinko

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


    #21514
    AdditiveFX
    AdditiveFX
    Participant

    I have version 4.14.6, on codecanyon that’s the newest version, right?

    #21515
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Yes, that is the latest one. I checked the source on the attachment search class, and the attachment results shoul have the $r->content_type as ‘attachment’. If it’s ‘pagepost’, then it’s a different post type in that case.

    Try testing it by printing the post type to the title:

    add_filter('asp_results', 'asp_results_remove_unlinked_attachments');
    function asp_results_remove_unlinked_attachments($results) {
        foreach ($results as $k=>&$r) {
           $results[$k]->title = $r->post_type;
        }
        return $results;	
    }

    It shouldn’t be ‘attachment’, but something else (?).

    Best,
    Ernest Marcinko

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


    #21517
    AdditiveFX
    AdditiveFX
    Participant

    Hey,

    it works now with your proposed fix! The results are indeed of post-type “attachment”. Content type is still “pagepost” for everything. When I print the post-type somewhere I get sensible strings like “attachment”, “event”, “page” and “post”

    This is solved. Would be interesting to know why the first solution doesn’t work for me.

    I’m using the index table engine, I have several custom post types in WordPress, I have indexed all of them including file contents – Seems like a normal use of your plugin, doesn’t it? 🙂
    Btw. this is an awesome plugin! Really happy with it!

    #21518
    AdditiveFX
    AdditiveFX
    Participant

    To be clear, this is the working code:

    add_filter('asp_results', 'asp_results_remove_unlinked_attachments');
    function asp_results_remove_unlinked_attachments($results) {
        foreach ($results as $k=>&$r) {
            if ( isset($r->post_type) && $r->post_type == 'attachment' ) {
    			$parent_id = wp_get_post_parent_id( $r->id );
                if ( is_wp_error($parent_id) || empty($parent_id) ) {
                    // Remove the post from the results list	
    				unset($results[$k]);
    			}
            }
        }
    	return $results;	
    }
    #21519
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    That’s it, the index table engine, I was looking at the wrong file there 🙂
    There is a bug in there, the content_type is not set correctly, I will make sure to fix that in the upcoming release. Glad to know the second one works 🙂

    Best,
    Ernest Marcinko

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


    #21520
    AdditiveFX
    AdditiveFX
    Participant

    perfect! Thanks for your help

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

You must be logged in to reply to this topic.