Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2016-05-14 16:34:09

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Export articles from a particular category in csv

Is there a way to export articles from a particular category using csv format? ie articles that appear on site.com/category/mycategory/ in Title|Body|Published Date format


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#2 2016-05-14 16:57:49

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,304

Re: Export articles from a particular category in csv

Hi Yiannis, I’ve once bookmarked this ’typical’ jakob :)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#3 2016-05-14 17:32:09

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,596
Website

Re: Export articles from a particular category in csv

@uli: Hehe, your memory is better than mine.

A couple of additions:

Recently, I needed to put together a fairly large content inventory of site data that I had brought over from WP to txp, and built a small helper page template to output csv-format on a per-section basis. You could easily adapt that to filter by category instead of section and output only the fields you need. It’s semi-manual but you have good control:

<txp:rah_gps name="out" />
<txp:if_variable name="out" value="">
No "out" variable specified
<txp:else />

<txp:if_variable name="out" value="images">
"id","ext","name","date","width","height","caption",
<txp:images limit="999" break="" sort="id asc">
"<txp:image_info type="id" />","<txp:image_info type="ext" />","<txp:image_info type="name" />","<txp:image_date format="%Y-%m-%d" />","<txp:image_info type="w" />","<txp:image_info type="h" />","<txp:image_info type="caption" />",</txp:images>

<txp:else />

<txp:article_custom section='<txp:variable name="out" />' limit="9999" break="">
<txp:rah_function call="trim">

<txp:if_variable name="out" value="filmmaker">
<txp:if_first_article>"section","url-stub","title","date","images","role","role_custom","projects",</txp:if_first_article>
"/<txp:section />","/<txp:article_url_title />","<txp:title no_widow="0" />","<txp:posted format="%Y-%m-%d" />","<txp:custom_field name="article_image" />","<txp:category1 title="1" />","<txp:custom_field name="filmmaker_role" />","<txp:custom_field name="filmmaker_projects" />",
</txp:if_variable>

<txp:if_variable name="out" value="project">
<txp:if_first_article>"section","url-stub","title","date","images","video","director","filmmakers","funding_awarded","project_phase","awards","quotes","homepage","email","facebook","twitter","legacy_wp_id",</txp:if_first_article>
"/<txp:section />","/<txp:article_url_title />","<txp:title no_widow="0" />","<txp:posted format="%Y-%m-%d" />","<txp:custom_field name="article_image" />","<txp:custom_field name="article_video" />","<txp:custom_field name="project_director" />","<txp:custom_field name="project_filmmakers" />","<txp:custom_field name="funding_awarded" />","<txp:custom_field name="project_phase" />","<txp:if_custom_field name="premieres_awards" value="">no<txp:else />yes</txp:if_custom_field>","<txp:if_custom_field name="project_quotes" value="">no<txp:else />yes</txp:if_custom_field>","<txp:custom_field name="project_homepage" />","<txp:custom_field name="project_email" />","<txp:custom_field name="project_facebook" />","<txp:custom_field name="project_twitter" />","<txp:custom_field name="legacy_id" />",
</txp:if_variable>

<txp:if_variable name="out" value="news">
<txp:if_first_article>"section","url-stub","title","date","images","video","categories","legacy_wp_id",</txp:if_first_article>
"/<txp:section />","/<txp:article_url_title />","<txp:title no_widow="0" />","<txp:posted format="%Y-%m-%d" />","<txp:custom_field name="article_image" />","<txp:custom_field name="article_video" />","<txp:category1 /><txp:if_article_category number="2">, <txp:category2 /></txp:if_article_category>","<txp:custom_field name="legacy_id" />",
</txp:if_variable>

</txp:rah_function></txp:article_custom>
</txp:if_variable>

</txp:if_variable>

Usage: assign the page template to a section, e.g. “output” and call with www.domain.com/output?out=section-name.

I then simply opened the page source (i.e. not the screen output) and copy-and-pasted it into a text editor and then saved as .csv. There may be a bit of minor cleaning to do. If your country doesn’t use a comma as separator for csv’s (in Germany, it’s typically a semi-colon) you can change the commas into the separator you need. Note: this was for personal internal use and not publicly accessible, so I didn’t bother with any security measures – if you have something like this publicly accessible, you should implement filters for the “out” variable and maybe other access measures.

rah_gps and rah_function are not essential: rah_gps is only used as switch so I could get different per-section output with the same template. And rah_function call="trim" just gets rid of extra spaces, which you could instead edit out later in the text editor. If you find that quotes in your content are breaking the output, try wrapping that item in <txp:rah_function call="txpspecialchars">…</txp:rah_function> and that should escape quotes in your content.

And if you need that to be part of your site’s function, you could also examine doing your export to csv with rah_external_output and setting the corresponding mime-type for creating a csv file.


TXP Builders – finely-crafted code, design and txp

Offline

#4 2016-05-15 08:31:21

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: Export articles from a particular category in csv

Thanks guys… I’ll start experimenting and come back here when I hit a wall.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

Board footer

Powered by FluxBB