Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-04-16 09:39:39

caruso_g
Member
From: Catania
Registered: 2009-04-11
Posts: 88
Website

How to exclude articles AND/OR categories from search results

Hi all, I am trying to build a search page from whose results I would like to filter some categories and show only link to sections.
The problem is that I created a one level website (i.e. /domain/section/) and all articles are shown as part of the “main” landing page whose address is the section address.
E.G.
I have a section “product_name”, its landing page is composed by one sticky article and several live articles (shown as tabs) I also have an article used for the sidebar content.
Doing a search now it shows me the “product_name” link, the “tabs_articles” and the “sidebar_articles” as well. And all are linked to their own article’s permalink such as /section_name/product_name/ or /section_name/sidebar_article/
Is there a way to filter all of those sub-articles/page-components AND/OR at least link them to their section instead of their own permalink?
As always, thank you all.
Giuseppe

Offline

#2 2011-04-16 19:48:29

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: How to exclude articles AND/OR categories from search results

You can edit the search_results form: replace the permlink tag with something like this:

<txp:if_article_section name="product_name">
<txp:section>article title or section title here</txp:section>
</txp:if_article_section>

If you want to exclude categories, you can add if_article_category tags as well.

Edit: if you are going to exclude categories this way, your search results count won’t be correct, as it will still count the articles found in these categories.

Last edited by els (2011-04-16 19:52:00)

Offline

#3 2011-04-21 13:50:09

caruso_g
Member
From: Catania
Registered: 2009-04-11
Posts: 88
Website

Re: How to exclude articles AND/OR categories from search results

Ciao Els, thanks a lot (as always!).
I edited your code, to make it a little bit more abstract, like:

<txp:if_article_section name='<txp:section_list break=","><txp:section /></txp:section_list>'>
<txp:section title="1" link="1" wraptag="h3" class="search_result_title" />
</txp:if_article_section>
<p class="entry-summary"><txp:search_result_excerpt /></p>

and it works greatly, thanks again.
The only issue now I have is that there are several matching results for the same sections (remember that any “page” is just the section, composed by several articles).
Is there a way to filter those results and show only one article per section?

Thanks you again :)
Giuseppe

Offline

#4 2011-04-21 19:01:53

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: How to exclude articles AND/OR categories from search results

caruso_g wrote:

Is there a way to filter those results and show only one article per section?

Ciao Giuseppe, I’m not sure, I would need to think about that.

But I’m wondering what this is supposed to do:

<txp:if_article_section name='<txp:section_list break=","><txp:section /></txp:section_list>'>

If this is the exact tag you are using, isn’t it basically saying ‘if any article section’? In that case, you wouldn’t need it at all…

Offline

#5 2011-04-21 20:33:17

caruso_g
Member
From: Catania
Registered: 2009-04-11
Posts: 88
Website

Re: How to exclude articles AND/OR categories from search results

Yep, it’s because I need to exclude some section, so this way (just adding exclude="section1,section2") I Will be albe to skip just them. Is it correct?

Offline

#6 2011-04-21 22:28:42

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: How to exclude articles AND/OR categories from search results

Ah, I see. Yes, that’s the easiest way to do it as far as I can see.

I haven’t thought of a way to show only one search result per section, but I wonder if something like this would also be an option for you:

<txp:if_article_section name='<txp:section_list exclude="whatever" break=","><txp:section /></txp:section_list>'>
<txp:if_different>
<txp:section title="1" link="1" wraptag="h3" class="search_result_title" />
</txp:if_different>
<p class="entry-summary"><txp:search_result_excerpt /></p>
<txp:else />
<h3><txp:permlink><txp:title /></txp:permlink></h3>
<p class="entry-summary"><txp:search_result_excerpt /></p>
</txp:if_article_section>

It shows the excerpts of all search results in a section, but only one link to the section page. You need a sort="section" in your article tag, otherwise the if_different won’t work.

Offline

#7 2011-04-22 13:19:20

caruso_g
Member
From: Catania
Registered: 2009-04-11
Posts: 88
Website

Re: How to exclude articles AND/OR categories from search results

Ahhhh! I was almost there but I didn’t know the sorting issue! :)

(some time later…)
Ok, it does work but… it shows me some php code I embedded into my articles… >:(
Is there a way to avoid showing that code? I know that I can just exclude sections from search, but I need those sections to be searchable.
So, can one just exclude specific articles?

Lastly, I have some articles that are just empty (could be I used just custom fields, or could be they are used just as construction parts of the page) is there some if_ tags to check for search_result_excerpt?

C’mon, we are almost there! :D

Offline

#8 2011-04-22 15:06:57

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: How to exclude articles AND/OR categories from search results

caruso_g wrote:

So, can one just exclude specific articles?

If you have a spare custom field, you could use it to indicate non-searchable articles, i.e. put ‘no’ in it if it shouldn’t show up in the search results, leave it empty if it should. Use (if the custom field is named ‘searchable’) searchable="" in the article tag.

Lastly, I have some articles that are just empty (could be I used just custom fields, or could be they are used just as construction parts of the page) is there some if_ tags to check for search_result_excerpt?

Yes, but don’t you want to exclude these articles from the search as well? In that case use the custom field again. If you still want to show the article title but avoid empty <p> tags, you can use variable/if_variable.

Search_results form (not entirely tested):

<txp:variable name="search_excerpt" value='<txp:search_result_excerpt />' />

<txp:if_article_section name='<txp:section_list exclude="whatever" break=","><txp:section /></txp:section_list>'>
   <txp:if_different>
      <txp:section title="1" link="1" wraptag="h3" class="search_result_title" />
   </txp:if_different>
   <txp:if_variable name="search_excerpt" value="">
   <txp:else />
      <p class="entry-summary"><txp:search_result_excerpt /></p>
   </txp:if_variable>
<txp:else />
   <h3><txp:permlink><txp:title /></txp:permlink></h3>
   <txp:if_variable name="search_excerpt" value="">
   <txp:else />
      <p class="entry-summary"><txp:search_result_excerpt /></p>
   </txp:if_variable>
</txp:if_article_section>

Offline

#9 2011-04-23 09:24:29

caruso_g
Member
From: Catania
Registered: 2009-04-11
Posts: 88
Website

Re: How to exclude articles AND/OR categories from search results

Hi Els, I thought about it, and I think I would like to have those articles searchable (e.g. one of those snippets of code renders apps changelogs that should be searchable) so it would be great to be able to perform searches on them too. I, then, created a radio button custom field thanks to glz_custom_field excellent plugin with name="searchable" and values "yes" and "no".
The problem is that with the following code, written to keep articles searchables but hiding code to malicious users, it doesn’t work:

<txp:variable name="search_excerpt" value='<txp:search_result_excerpt />' />

<txp:if_variable name="search_excerpt" value="">
<txp:else />
<li>
    <txp:if_article_section name='<txp:section_list break="," exclude="search,site-structure,template,webstore"><txp:section /></txp:section_list>'>
        <txp:if_different>
            <txp:section title="1" link="1" wraptag="h3" class="search_result_title" />
        </txp:if_different>
    </txp:if_article_section>
    <txp:if_custom_field name="searchable" value="no">
        <p class="search_result_excerpt">…<span><txp:search_term /></span>…</p>
    <txp:else />
        <p class="search_result_excerpt"><txp:search_result_excerpt hilight="span" /></p>
    <txp:if_custom_field>
</li>
</txp:if_variable>

The code calling the search results is:

<txp:if_search>
            <h1 id="page_title" class="grid_12"><txp:text item="search_results" /></h1>
            <h2 id="tagline" class="grid_12">You searched for: "<txp:page_url type="q" />"</h2>
            <txp:article searchsticky="1" limit="10" sort="section" wraptag="ul" class="grid_12 search_results" break="" searchform="bs_search_results" />                
       <txp:else />

Lastly, the following is the tag trace related to a “changelog” query:

<txp:if_section name=''>
	[<txp:if_section name=''>: true]
	<txp:if_search>
		[<txp:if_search>: true]
		<txp:text item="search_results" />
		<txp:page_url type="q" />
		<txp:article searchsticky="1" limit="10" sort="section" wraptag="ul" class="grid_12 search_results" break="" searchform="bs_search_results" />
			[SQL (0.00053286552429199): select name from txp_section where searchable != '1']
			[SQL (0.002068042755127): select count(*) from textpattern where 1=1 and Status >= 4 and Posted <= now() and (now() <= Expires or Expires = '0000-00-00 00:00:00') and (`Title` like '%changelog%' or `Body` like '%changelog%') and Section != 'webstore' and Section != 'site-structure' and Section != 'search' and Section != 'template']
			[SQL (0.0026359558105469): select *, unix_timestamp(Posted) as uPosted, unix_timestamp(Expires) as uExpires, unix_timestamp(LastMod) as uLastMod, match (`Title`, `Body`) against ('changelog') as score from textpattern where 1=1 and Status >= 4 and Posted <= now() and (now() <= Expires or Expires = '0000-00-00 00:00:00') and (`Title` like '%changelog%' or `Body` like '%changelog%') and Section != 'webstore' and Section != 'site-structure' and Section != 'search' and Section != 'template' order by section limit 0, 10]
			[article 11]
			[SQL (0.00037884712219238): select Form from txp_form where name='bs_search_results']
			[Form: bs_search_results]
			<txp:variable name="search_excerpt" value='<txp:search_result_excerpt />' />
				[attribute 'value']
				<txp:search_result_excerpt />
				[/attribute]
			[article 15]
			[Form: bs_search_results]
			<txp:variable name="search_excerpt" value='<txp:search_result_excerpt />' />
				[attribute 'value']
				<txp:search_result_excerpt />
				[/attribute]
			[article 19]
			[Form: bs_search_results]
			<txp:variable name="search_excerpt" value='<txp:search_result_excerpt />' />
				[attribute 'value']
				<txp:search_result_excerpt />
				[/attribute]
	</txp:if_search>
</txp:if_section>
<txp:if_search>
	[<txp:if_search>: true]
</txp:if_search>

I don’t know why… Of course, I set the custom field of two changelog articles one to “yes” and one to “no” for testing purpose.
Thank you in advance for your help.

EDIT
<txp:shame>
Ok, just as a monition to myself for the next time to check my code 100,001 instead of just 100,000, I am not going to delete this stupid post…
Solution:
I didn’t closed the </txp:if_custom_field>
</txp:shame>

Last edited by caruso_g (2011-04-23 09:31:13)

Offline

#10 2011-04-23 09:58:21

caruso_g
Member
From: Catania
Registered: 2009-04-11
Posts: 88
Website

Re: How to exclude articles AND/OR categories from search results

Ok, this is for real…
Why this if_search_results is not working?

<txp:if_search_results>
    <txp:variable name="search_excerpt" value='<txp:search_result_excerpt />' />

    <txp:if_variable name="search_excerpt" value="">
    <txp:else />
        <li>
            <txp:if_article_section name='<txp:section_list break="," exclude="search,site-structure,template,webstore"><txp:section /></txp:section_list>'>
                <txp:if_different>
                    <txp:section title="1" link="1" wraptag="h3" class="search_result_title" />
                </txp:if_different>
            </txp:if_article_section>
            <txp:if_custom_field name="searchable" value="no">
                <p class="search_result_excerpt">…<span><txp:search_term /></span>…</p>
            <txp:else />
                <p class="search_result_excerpt"><txp:search_result_excerpt hilight="span" /></p>
            </txp:if_custom_field>
        </li>
    </txp:if_variable>
<txp:else />
    <li>
        <p>We are sorry, but your search provided no results</p>
        <p>Please, take a look at the sitemap in the footer</p>
    </li>
</txp:if_search_results>

Offline

#11 2011-04-23 11:42:36

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: How to exclude articles AND/OR categories from search results

Glad you figured it out :)

Ok, this is for real…
Why this if_search_results is not working?

‘Not working’ meaning not displaying the ‘else’ part when you would expect it? Did you check if in those cases there really are zero search results, or there are, but they are just not being displayed?

Offline

#12 2011-04-23 20:00:47

caruso_g
Member
From: Catania
Registered: 2009-04-11
Posts: 88
Website

Re: How to exclude articles AND/OR categories from search results

:D I am sorry… yep, it doesn’t show the else statement.
I tested it with terms that I am sure are not in articles but nothing… :(
Any idea?

Offline

Board footer

Powered by FluxBB