Articles Anywhere

Limit articles by date...

2 years 5 months ago #63506

Paul Werten's Avatar Paul Werten

Hi Peter,

A simple question, I hope 😉 :

Is it possible to limit the articles displayed/selected by Articles Anywhere Pro via publication date?

What I want to achieve is to display only articles that came out this month, either by supplying a start and end date, or the number of days back from today (e.g. looking 30 days in the past).

Thx, Paul

2 years 5 months ago #63507

Peter van Westen's Avatar Peter van Westen Admin

Complex date calculations are not possible inside Articles Anywhere itself.
An alternative would be to just simply show the last x number of articles.

Otherwise you would need to do a custom php database search and output via Sourcerer.
Or look for a module that does what you want and optionally output it inside an article with Modules Anywhere.

2 years 5 months ago #63509

Paul Werten's Avatar Paul Werten

OK, thanks... I'll try to figure something out. I had just hoped that it would be possible to realize with articles anywhere directly. Would be a cool feature 😎!

(I think you once responded quite similarly about tags, and yet they magically appeared in articles anywhere; one can always hope I guess 😉)

Thx, Paul

2 years 1 day ago #70051

Paul Werten's Avatar Paul Werten

Hi Peter,

Is there anything new in this direction (filtering by date; like one does for tags, for example)?

I came across this need again (only display articles of a certain category, with a created date after today) and am still wondering how best to approach this with articles anywhere/IFs/sourcerer...

Would really welcome any ideas or pay for a simple solution if needed!

Thx, Paul

2 years 12 hours ago #70063

Peter van Westen's Avatar Peter van Westen Admin

There are no options in Articles Anywhere to filter by date ranges.

In Sourcerer you could do something like:

{source}<?php
$from = '2017-03-22';
$to   = '2017-04-22';

$query = $db->getQuery(true)
	->select($db->quoteName('a.id'))
	->from($db->quoteName('#__content'))
	->where($db->quoteName('created') . ' >= ' . $db->quote($from))
	->where($db->quoteName('created') . ' <= ' . $db->quote($to));

$db->setQuery($query);

$ids = $db->loadColumn();

echo '{' . 'articles ids="' . implode(',', $ids) . '"}'
	. '<h2>[title]</h2>'
	. '<p>[introtext]</p>'
	. '... whatever other data/html you want ...'
	. '{/' . 'articles}';
 ?>{/source}

2 years 11 hours ago #70068

Paul Werten's Avatar Paul Werten

Hi Peter,

Thanks a lot for this!

I'm trying to combine with the PHP date() function now and my HTML code for display; will let you know if it worked!

Thanks again!

Paul

2 years 11 hours ago #70070

Paul Werten's Avatar Paul Werten

Hi Peter,

I tried the following:

{source}
	<?php
	$from = '2016-03-22';
	$to   = '2222-22-22';

	$query = $db->getQuery(true)
		->select($db->quoteName('a.id'))
		->from($db->quoteName('#__content'))
		->where('( ' . $db->quoteName('created') . ' >= ' . $db->quote($from) . ' )')
		->where('( ' . $db->quoteName('created') . ' <= ' . $db->quote($to);

	$db->setQuery($query);

	$ids = $db->loadColumn();

	echo '{' . 'articles ids="' . implode(',', $ids) . '"}'
		. '<div style="text-align:left;vertical-align:top;width:100%;font-size:0px">'
		. '<div style="display:inline-block;vertical-align:top;width:40%;padding:1px;background-color:black">'
		. '<a href="{url}"><img src="/resized/w306h165/{image_intro}" alt="{title:strip}" style="width:100%;border 1px solid black"></a>'
		. '</div>'
		. '<div style="display:inline-block;vertical-align:top;padding-left:20px;width:60%">'
		. '<p class="mix-introtext" style="font-size:16px;font-weight:400;margin-bottom:0px">'
		. '<a href="{url}">{title:66:strip}</a><br><span style="font-weight:300">{introtext:88:strip}</p>'
		. '<p class="mix-introtext" style="font-size:14px;margin-bottom:15px"><span style="color:#888888">Published: {created format="d.m.Y"}</span></p>'
		. '<a class="btn btn-default" href="{url}"><span>Read more</span></a>'
		. '</div>'
		. '</div>'
		. '<div style="height:20px"></div>';
	?>
{/source}

But I get the following error:

0
syntax error, unexpected ';', expecting ',' or ')'
It appears that the page you were looking for does not exist.
Other things to try
Go back home
If you came here by mistake, try going back to home page.

Any ideas?

Thx, Paul

2 years 11 hours ago #70071

Paul Werten's Avatar Paul Werten

Also, can I filter the id list from your code for a category by just adding categories="..." to the {articles} part, or wouldn't that work?

Paul

2 years 10 hours ago #70072

Peter van Westen's Avatar Peter van Westen Admin

There are some syntax issues in your code.
Try below (including category filter):

	$catids = [12, 23, 34]; // Array of category ids

	$query = $db->getQuery(true)
		->select($db->quoteName('a.id'))
		->from($db->quoteName('#__content'))
		->where($db->quoteName('catid') . ' IN (' . implode(',' $catids) . ')')
		->where($db->quoteName('created') . ' >= ' . $db->quote($from))
		->where($db->quoteName('created') . ' <= ' . $db->quote($to));

PS: Don't forget to output the closing {/articles} tag.

2 years 10 hours ago #70073

Paul Werten's Avatar Paul Werten

Hi Peter,

Thanks again for your fast support and for combining the categories in the query!

I now use:

{source}

	<?php
	$catids = [19]; // Array of category ids
	$from = date("Y-m-d");
	$to   = '2222-22-22';

	$query = $db->getQuery(true)
		->select($db->quoteName('a.id'))
		->from($db->quoteName('#__content'))
		->where($db->quoteName('catid') . ' IN (' . implode(',' $catids) . ')')
		->where($db->quoteName('created') . ' >= ' . $db->quote($from))
		->where($db->quoteName('created') . ' <= ' . $db->quote($to));
	
	$db->setQuery($query);

	$ids = $db->loadColumn();

	echo '{' . 'articles ids="' . implode(',', $ids) . '"}'
		. '<div style="text-align:left;vertical-align:top;width:100%;font-size:0px">'
		. '<div style="display:inline-block;vertical-align:top;width:40%;padding:1px;background-color:black">'
		. '<a href="{url}"><img src="/resized/w306h165/{image_intro}" alt="{title:strip}" style="width:100%;border 1px solid black"></a>'
		. '</div>'
		. '<div style="display:inline-block;vertical-align:top;padding-left:20px;width:60%">'
		. '<p class="mix-introtext" style="font-size:16px;font-weight:400;margin-bottom:0px">'
		. '<a href="{url}">{title:66:strip}</a><br><span style="font-weight:300">{introtext:88:strip}</p>'
		. '<p class="mix-introtext" style="font-size:14px;margin-bottom:15px"><span style="color:#888888">Published: {created format="d.m.Y"}</span></p>'
		. '<a class="btn btn-default" href="{url}"><span>Read more</span></a>'
		. '</div>'
		. '</div>'
		. '<div style="height:20px"></div>'
		. '{/' . 'articles}';
	?>
	
{/source}

And get:

syntax error, unexpected '$catids' (T_VARIABLE), expecting ',' or ')'
It appears that the page you were looking for does not exist.


Is it because I now just have one category in stead of an array, or is it something else?

2 years 10 hours ago #70075

Peter van Westen's Avatar Peter van Westen Admin

Sorry, missing comma in the implode:

implode(',', $catids)

But if you don't know how php works, you should probably not be using Sourcerer.

2 years 9 hours ago #70076

Paul Werten's Avatar Paul Werten

Hi Peter,

I approach this quite pragmatically, really:

I have a module that does/did all the above, but it has proven to be unreliable under certain conditions and I would rather replace it altogether, as long as I can keep the functionality it offers. The combination of Sourcerer and Articles Anywhere allows me to do just that, and as long as I can get enough of the code to change it to my needs, I think it's fine.

Anyway, thanks again for your time! It is much appreciated.

I'll do the remaining PHP debugging with our external IT partner...

Cheers, Paul

2 years 4 hours ago #70081

Paul Werten's Avatar Paul Werten

Hi Peter,

I finally managed to get everything running using the following code:

<div class="no-margins">

{source}
	<?php
	$catids = [19];
	$from = date("Y-m-d");
	$to   = '2017-12-31';

	$query = $db->getQuery(true)
		->select($db->quoteName('id'))
		->from($db->quoteName('#__content'))
		->where($db->quoteName('catid') . ' IN (' . implode(',', $catids) . ')')
		->where($db->quoteName('created') . ' >= ' . $db->quote($from))
		->where($db->quoteName('created') . ' <= ' . $db->quote($to));
	
	$db->setQuery($query);

	$ids = $db->loadColumn();

	echo '{' . 'articles ' . implode(',', $ids) . ' ordering\="created ASC"}';
  ?>
{/source}
<div style="text-align:left;vertical-align:top;width:100%;font-size:0px">
	    <div style="display:inline-block;vertical-align:top;width:40%;padding:1px;background-color:black">
          <a href="{url}">
    	    <img src="/resized/w306h165/{image_intro}" alt="{title:strip}" style="width:100%;border 1px solid black">
		  </a>    
        </div>
        <div style="display:inline-block;vertical-align:top;padding-left:20px;width:60%">
          <p class="mix-introtext" style="font-size:16px;font-weight:400">
            <a href="{url}">
               {title:66:strip}
            </a> 
          <br>
            <span style="font-weight:300">{introtext:88:strip}</span></p>
          <a class="btn btn-default" href="{url}"><span>Read more</span></a>
        </div>
</div>
<div style="height:20px"></div>{/articles}  
</div>

The only remaining problem is, that Articles Anywhere does not sort articles (ordering="created ASC" in my case) when an array of article IDs is supplied...

Is there any way around this?

Thanks a lot, Paul

2 years 4 hours ago #70082

Paul Werten's Avatar Paul Werten

OK, I changed the {articles} echo code in Sourcerer to include articles="..." and then thought the sorting would do something, but alas...

Code:

echo '{' . 'articles articles\="' . implode(',', $ids) . '" ordering\="created ASC"}';

Can the results be sorted during the PHP query instead?

Thanks, Paul

2 years 3 hours ago #70083

Paul Werten's Avatar Paul Werten

YES!!!

This did the trick (see, I'm learning 😉)

$query = $db->getQuery(true)
		->select($db->quoteName('id'))
		->from($db->quoteName('#__content'))
		->where($db->quoteName('catid') . ' IN (' . implode(',', $catids) . ')')
		->where($db->quoteName('created') . ' >= ' . $db->quote($from))
		->where($db->quoteName('created') . ' <= ' . $db->quote($to))
		->order('created ASC');

2 years 3 hours ago #70088

Peter van Westen's Avatar Peter van Westen Admin

2 years 3 hours ago #70092

Paul Werten's Avatar Paul Werten

Thanks!

As a final comment here: Sourcerer really is a very powerful tool (as all your tools are, actually)!

Cheers, Paul