I got found out how to make this work. Here is a very comprehensive guide:
Purpose
To exclude pages from the search results based upon their root parrent.
Method
This will not exclude those pages from being indexed, but rather, this guide will simply hide them from the results page. This will get a list of aliases of those pages that we want to exclude. This will use a hierarchical position to govern which pages will be excluded. For example, if you use 1 as your hierarchical position, you will exclude all pages that have a position that is greater than or equal to 1 and less than 2 (>= 1 && url of every result.
Steps
1. Create a UDT with this code called all_content:
Code: Select all
/*$params['assign']*/
/*$params['parent_hierarchy']*/
if (!empty($params['assign']) && !empty($params['parent_hierarchy'])){
global $gCms;
$contentops =& $gCms->GetContentOperations();
$all_content = $contentops->GetAllContent(false);
$assign = array();
foreach($all_content as $one_page){
if (strpos($one_page->mHierarchy, $params['parent_hierarchy']) === 0){
$assign[] = $one_page->mAlias;
}
}
$smarty = $gCms->GetSmarty();
$smarty->assign($params['assign'], $assign);
}
Thanks to Peciura for replying to my other post:
http://forum.cmsmadesimple.org/index.php/topic,41759.0.html.
2. Put this at the very top of your search results template:
3. Call the UDT in your search results template like this (where parrent_hierarchy is the position you want to exclude, in this case, children of page 1):
{all_content assign='children' parent_hierarchy='00001'}
The tag should be called directly after the {foreach from=$results item=entry} tag. So it should look like this:
{foreach from=$results item=entry}
{all_content assign='children' parent_hierarchy='00001'}
This causes the all_content tag to be called once per search result entry.
4. Put this code directly after {all_content assign='children' parent_hierarchy='00001'}:
Code: Select all
{foreach from=$children item=child}
{cms_selflink href=$child assign='childurl'}
{if $childurl == $entry->url}
{assign var="show" value="no"}
{/if}
{/foreach}
5. After that, you need to make an {if} statement like this:
{if $show != "no"}
[Some code to display the search results]
{else}
{assign var="nocount" value=$nocount+1}
{/if}
This might look something like this:
Code: Select all
{if $show != "no"}
<li><a href="{$entry->url}">{$entry->title}</a> ({$entry->weight}%){$entry->id}</li>
{else}
{assign var="nocount" value=$nocount+1}
{/if}
6. Directly after the {if $show != "no"} .............. {/if} statement, we need to reset the show variable to something besides "no". Add this code directly after the {if $show != "no"} .............. {/if}:
7. By default, you will have something like this in your results template:
{if $itemcount > 0}
[Some code to display the search results]
{else}
{$noresultsfound}
{/if}
You need to get rid of the else condition, so you will have something like this:
{if $itemcount > 0}
[Some code to display the search results]
{/if}
The reason for this is because the $itemcount will still be greater than 0 even if all the items are from the excluded pages, and thus hidden. So after the end of {if $itemcount > 0} ...................... {/if} statement, you need to add this (assuming you want your $noresultsfound message in bold and in the element):
Code: Select all
{if $nocount == $itemcount}
<p><strong>{$noresultsfound}</strong></p>
{/if}
The $nocount variable is how many results have been excluded. If this is equal to the total number of results ($itemcount), then that means that there are no search results. This holds true when $itemcount is equal to 0, because $nocount is also equal to 0.
The Code
In Syntax (the bare minimum code, assuming that you have created the UDT, see step 1):
{assign var="nocount" value=0}
{if $itemcount > 0}
{foreach from=$results item=entry}
{all_content assign='children' parent_hierarchy='
[the hierarchical position root that you want to exclude]'}
{foreach from=$children item=child}
{cms_selflink href=$child assign='childurl'}
{if $childurl == $entry->url}
{assign var="show" value="no"}
{/if}
{/foreach}
{if $show != "no"}
Code: Select all
[/i][/b]
{else}
{assign var="nocount" value=$nocount+1}
{/if}
{assign var="show" value=""}
{/foreach}
{/if}
{if $nocount == $itemcount}
[b][i][Code to show that no results were found. Usually {$noresultsfound}][/i][/b]
{/if}
In example (adapted to the default result template using 1 as the hierarchy # to exclude):
[code]{assign var="nocount" value=0}
{if $itemcount > 0}
<ul>
{foreach from=$results item=entry}
{all_content assign='children' parent_hierarchy='00001'}
{foreach from=$children item=child}
{cms_selflink href=$child assign='childurl'}
{if $childurl == $entry->url}
{assign var="show" value="no"}
{/if}
{/foreach}
{if $show != "no"}
<li><a href="{$entry->url}">{$entry->title}</a> ({$entry->weight}%){$entry->id}</li>
{*
You can also instantiate custom behaviour on a module by module basis by looking at
the $entry->module and $entry->modulerecord fields in $entry
ie: {if $entry->module == 'News'}{News action='detail' article_id=$entry->modulerecord detailpage='News'}
*}
{else}
{assign var="nocount" value=$nocount+1}
{/if}
{assign var="show" value=""}
{/foreach}
</ul>
{else}
<ul></ul>
{/if}
{if $nocount == $itemcount}
<p><strong>{$noresultsfound}</strong></p>
{/if}
Notes
[Important] When I say "children" of a certain page, that includes the page itself. So If I say children of page 1, that will include page 1.
[Minor] If you have a setup that has all the search results listed in a element, you may need to have an else condition for {if $itemcount > 0}, that would be {else} because if all the results are excluded (your $itemcount is more than 0, but all the items are excluded), you will still have a even though there are no show results, because there are still "entries." But if there are no entries, then you will not have that unless you add it manually with an else condition. This will change the look of the front end a little bit.