• Resolved Devotoare

    (@sts_jon)


    First, amazing plugin.

    Second, would it be possible to add an option to display the top-most parent page of the search result (with a separator like pipe). I have found the code to get the top parent, but I do not know how/where to place it within the plugin.

    Code for retrieving top-most parent:

    $parents = get_post_ancestors( $post );
    echo get_the_title( end($parents) );

    Example:
    Parent Page | Child Page One – Subject

    Thank you for any help and amazing work!

    https://www.ads-software.com/plugins/search-autocomplete/

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Gabe Shackle

    (@hereswhatidid)

    Do you want the links to be active to each of the pages (Parent and Child) or do you just want to indicate page hierarchy in the dropdown list?

    Thread Starter Devotoare

    (@sts_jon)

    I want the links how they are (hotlinking to the result) I just want users to be able to see the Parent (or grandparent as the case may be) so that:

    <a href="childpage">TopPage | Page Link</a>

    No need for 2 links in 1 line (if that is what your asking).

    I want this because I have a site with 200+ pages, some of them have the exact same name (but different content) but fall under different Top-Most parents, so I would like end users to be able to make sure they are getting the result they need.

    Thread Starter Devotoare

    (@sts_jon)

    I think the short answer would have been “indicate page hierarchy in the dropdown list”… sorry. ??

    Edit:
    After some thought the option for top-most or full hierarchy would be nice… before I was only considering top-most page, but since you mentioned hierarchy I was think a full list would be good

    Plugin Author Gabe Shackle

    (@hereswhatidid)

    I think this is a bit outside of the default functionality I’d like to have included BUT this would be something that should able to be handled via one of the filters I’ve created. Currently they just offer the string values (title, url) but I will push out an update this evening that will also include the actual post/taxonomy object so that you could then easily add those hierarchy elements in any fashion that you’d like.

    Look for an update tonight or tomorrow evening and I’ll post a sample code here for how you could get the result you’re looking for via the filters.

    Thread Starter Devotoare

    (@sts_jon)

    Thank you so much!! That is awesome support!

    Plugin Author Gabe Shackle

    (@hereswhatidid)

    I’ve just pushed the updated that includes the new term/post object along with the link and url filters. Here’s a quick sample code to show the top level parent of a post (if it exists)

    add_filter( 'search_autocomplete_modify_title', 'saModifyTitle', 10, 2 );
    
    function saModifyTitle( $title, $object ) {
    	$result = $title;
    	if ( $object['type'] === 'post' ) {
    		$parents = get_post_ancestors( $object['id'] );
    		if ( count( $parents ) > 1 ) {
    			$result = get_the_title( end( $parents ) ) . ' - ' . $title;
    		}
    	}
    
    	return $result;
    }
    Thread Starter Devotoare

    (@sts_jon)

    I won’t get to try it out until tomorrow, but I thank you very much for the time it took to make this possible!

    Thread Starter Devotoare

    (@sts_jon)

    Just got a chance to try it, works great! Thank you so much!

    Heya, this is exactly what I’ve been looking for too. Where do I add that code?

    Plugin Author Gabe Shackle

    (@hereswhatidid)

    You can place that code in your theme’s functions.php or in a plugin.

    Will this work for Page titles too or is it just for posts?

    For example I’m setting WP for a games site that has various similarly titled pages. One commonly used page for each game is Characters which means in the search autocomplete it just shows 5 “Characters” pages without the title of parent page so it makes it very hard to distinguish which is which.

    Excellent solution – was wondering if we could instead display the post type instead of the parent post name i.e. Portfolio: Post Title; Blog Post: Post Title; Page: Post Title.

    Plugin Author Gabe Shackle

    (@hereswhatidid)

    Definitely, it’s essentially the same call. All you would need to do is instead of getting the ancestor you would want to get the post type:

    add_filter( 'search_autocomplete_modify_title', 'saModifyTitle', 10, 2 );
    
    function saModifyTitle( $title, $object ) {
    	$result = $title;
    
    	if ( $post_type = get_post_type( $object['id'] ) ) {
    		$result .= $post_type . ': ' . $title;
    	}
    
    	return $result;
    }
Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Add Top Parent To Results’ is closed to new replies.