Forum

Thread tagged as: Question, Problem, Blog

Filtering archives on the blog by section

Hi,

Is it possible to filter the archives on the blog by section? I currently have two sections with the blog app: 'blog' and 'our-work', but only want the posts from the 'blog' section to appear and be filtered.

I am able to filter the results by section, but where the archives are listed on the page by month and year, it does not filter by section – it displays all posts from both sections.

I've tried using some custom code with AJAX but could not get it to work; so I'm hoping there is a Perch solution to this!

Thanks in advance, K

Darren Cusdin

Darren Cusdin 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you post your diagnostics and the code you're using to display the archive?

Hi Drew,

Apologies on that, I should have included this to begin with!

I did include an array in the following using 'section' => 'blog:

<?php  perch_blog_date_archive_months('months_year_link.html', 'months_month_link.html'); ?>

But that seemed to break the code.

archive.php

<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/header.php'); ?>

<main class="archive">
      <div class="page_title">
          <h1>
            <?php perch_pages_title(); ?>
          </h1>
      </div>    
  <!-- /div.content-wrapper -->
  <div class="container">
      <div class="right_col">        
          <div class="archive">
           <a class="back" href="/blog/">Back to Blog</a>
            <h3>Archives</h3>

              <?php 
                  perch_blog_date_archive_months('months_year_link.html', 'months_month_link.html');
              ?>

          <?php// perch_blog_date_archive_months(); ?>

           <?php// perch_blog_custom([
                //'template' => '_archive.html',
                //'section' => 'blog',
                //'filter' => 'visible',
            //]);?>

               <?php perch_blog_categories([
                    'filter' => 'visible',
                    'section' => 'blog',
                    'include-empty' => false
                    ]); 
                ?>

               <?php //perch_blog_tags(); ?>
            <p>&nbsp; </p>
          </div>
      </div>
    <!-- /div.left-col -->

     <div class="post-col">
     <div class="left_col" id="archive_left">


     <?php
                // defaults for all modes
                $posts_per_page = 20;
                $template       = 'post_in_list.html';
                $sort_order     = 'DESC';
                $sort_by        = 'postDateTime';

                // Have we displayed any posts yet?
                $posts_displayed = false;

                /* --------------------------- POSTS BY TAG --------------------------- */
                if (perch_get('tag')) {
                    echo '<h1>Tag: '.perch_blog_tag(perch_get('tag'), true).'</h1>';

                    perch_blog_custom(array(
                            'tag'        => perch_get('tag'),
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            ));

                    $posts_displayed = true;
                }

                 /* --------------------------- POSTS BY CATEGORY --------------------------- */
                if (perch_get('cat')) {
                    echo '<h1>Category: '.perch_blog_category(perch_get('cat'), true).'</h1>';

                    perch_blog_custom(array(
                            'category'   => perch_get('cat'),
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            ));

                    $posts_displayed = true;
                }

                /* --------------------------- POSTS BY DATE RANGE --------------------------- */
                if (perch_get('year')) {

                    $year              = intval(perch_get('year'));
                    $date_from         = $year.'-01-01 00:00:00';
                    $date_to           = $year.'-12-31 23:59:59';
                    $title_date_format = '%Y';


                    // Month and Year?
                    if (perch_get('month')) {
                        $month             = intval(perch_get('month'));
                        $date_from         = $year.'-'.str_pad($month, 2, '0', STR_PAD_LEFT).'-01 00:00:00';
                        $date_to           = $year.'-'.str_pad($month, 2, '0', STR_PAD_LEFT).'-31 23:59:59';
                        $title_date_format = '%B, %Y';
                    }

                    echo '<h1>Archive: '.strftime($title_date_format, strtotime($date_from)).'</h1>';

                    perch_blog_custom(array(
                            'filter'     => 'postDateTime',
                            'match'      => 'eqbetween',
                            'value'      => $date_from.','.$date_to,
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            'section' => 'blog',
                            ));

                    $posts_displayed = true;
                }
?>
    </div>
    <!-- /div.post-col --> 

  <div class="content-swap">
  </div>
  <!-- /div.content-swap -->


  </div>
  </div>
  <!-- /div.content-wrapper --> 

</main>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/footer.php'); ?>

Here are my templates excluding the AJAX (as that didn't seem to work):

months_year_link.html:

<perch:before>
<ul>
</perch:before>
    <li><a href="archive.php?year=<perch:blog id="year" />"><perch:blog id="year" encode="false" /> (<perch:blog id="year_qty" />)</a>
    <perch:blog id="months" encode="false" />
    </li>
<perch:after>
</ul>
</perch:after>

months_month_link.html:

<perch:before>
<ul>
</perch:before>
    <li class="month"><a href="archive.php?year=<perch:blog id="postDateTime" format="Y" />&amp;month=<perch:blog id="postDateTime" format="m" />"><perch:blog id="postDateTime" format="%B" /> (<perch:blog id="month_qty" />)</a></li>
<perch:after>
</ul>
</perch:after>

Diagnostics report:

Perch: 2.8.32
Production mode: Production (100)
Installed apps: content (2.8.32), assets (2.8.32), categories (2.8.32), perch_blog (5.0), perch_forms (1.8.3)
DB driver: PDO
DB tables: perch2_blog_authors (3), perch2_blog_comments (0), perch2_blog_index (2980), perch2_blog_posts (19), perch2_blog_posts_to_tags (118), perch2_blog_sections (3), perch2_blog_tags (88), perch2_blogs (1), perch2_categories (5), perch2_category_counts (0), perch2_category_sets (2), perch2_content_index (1981), perch2_content_items (164), perch2_content_regions (40), perch2_forms (1), perch2_forms_responses (4), perch2_navigation (0), perch2_navigation_pages (0), perch2_page_templates (7), perch2_pages (26), perch2_resource_log (482), perch2_resource_tags (0), perch2_resources (174), perch2_resources_to_tags (0), perch2_settings (22), perch2_user_passwords (0), perch2_user_privileges (37), perch2_user_role_privileges (29), perch2_user_roles (2), perch2_users (5)
Users: 5
App runtimes:
<?php
    $apps_list = array(
        'content', 
        'categories',
        'perch_blog',
        'perch_forms',
    );

Scheduled tasks for perch_blog: delete_spam_comments (1440 mins)
Editor plug-ins: markitup
H1: 741f63c47c16b5492e524684db8d311e
L1: 819b8ab95f82746e1f0d6d296e4dddcd
F1: 2edba60ed1f613d6dd804feb202456a2
headerColour: #ffffff
content_singlePageEdit: 1
helpURL:
siteURL: /
hideBranding: 0
content_collapseList: 1
lang: en-gb
update_2.8.32: done
latest_version: 2.8.15
on_sale_version:
perch_blog_update: 5.0.1
perch_blog_post_url: /{sectionSlug}/{postSlug}
headerScheme: light
perch_blog_site_name:
perch_blog_slug_format: {postTitle}
perch_blog_akismet_key:
perch_blog_max_spam_days: 0
dashboard: 0
hide_pwd_reset: 0
content_hideNonEditableRegions: 0
content_frontend_edit: 0
perch_blog_comment_notify: 0
PERCH_DEVELOPMENT: 10
PERCH_STAGING: 50
PERCH_PRODUCTION: 100
PERCH_DB_USERNAME: energypr
PERCH_DB_SERVER: localhost
PERCH_DB_DATABASE: admin_energypr
PERCH_DB_PREFIX: perch2_
PERCH_TZ: UTC
PERCH_EMAIL_FROM: jamesh@thirdfloordesign.co.uk
PERCH_EMAIL_FROM_NAME: James Holmes
PERCH_LOGINPATH: /cms
PERCH_PATH: /var/www/vhosts/thirdfloordigital.net/energypr.thirdfloordigital.net/cms
PERCH_CORE: /var/www/vhosts/thirdfloordigital.net/energypr.thirdfloordigital.net/cms/core
PERCH_RESFILEPATH: /var/www/vhosts/thirdfloordigital.net/energypr.thirdfloordigital.net/cms/resources
PERCH_RESPATH: /cms/resources
PERCH_HTML5: 1
PERCH_DEBUG: 1
PERCH_RUNWAY:
PERCH_ERROR_MODE: DIE
PERCH_DATE_LONG: %d %B %Y
PERCH_DATE_SHORT: %d %b %Y
PERCH_TIME_SHORT: %H:%M
PERCH_TIME_LONG: %H:%M:%S
PERCH_RUNWAY_ROUTED:
PERCH_STRONG_PASSWORDS:
PERCH_PREVIEW_ARG: preview
PERCH_TEMPLATE_PATH: /var/www/vhosts/thirdfloordigital.net/energypr.thirdfloordigital.net/cms/templates
PERCH_DEFAULT_DOC: index.php
PERCH_DEFAULT_EXT: .php
PERCH_PRODUCTION_MODE: 100
PERCH_RWD:
PERCH_HTML_ENTITIES:
PERCH_SSL:
PERCH_STRIPSLASHES:
PERCH_PROGRESSIVE_FLUSH: 1
PERCH_PARANOID:
PERCH_FORCE_SECURE_COOKIES:
PERCH_PASSWORD_MIN_LENGTH: 6
PERCH_MAX_FAILED_LOGINS: 10
PERCH_AUTH_LOCKOUT_DURATION: 1 HOUR
PERCH_VERIFY_UPLOADS:
PERCH_AUTH_PLUGIN:
PERCH_DB_CHARSET: utf8
PERCH_DB_PORT:
PERCH_DB_SOCKET:
PERCH_SESSION_TIMEOUT_MINS: 20
HOSTING SETTINGS

PHP: 5.4.13
Zend: 2.4.0
OS: Linux
SAPI: cgi-fcgi
Safe mode: not detected
MySQL client: 5.5.30
MySQL server: 5.5.30
Free disk space: 41.83 GB
Extensions: Core, date, ereg, libxml, openssl, pcre, zlib, bz2, calendar, ctype, hash, filter, ftp, gettext, gmp, SPL, iconv, pcntl, readline, Reflection, session, standard, shmop, SimpleXML, sockets, mbstring, tokenizer, xml, cgi-fcgi, curl, dom, fileinfo, gd, imagick, imap, json, ldap, exif, mcrypt, mysql, mysqli, odbc, PDO, pdo_mysql, PDO_ODBC, pdo_sqlite, Phar, snmp, sqlite3, wddx, xmlreader, xmlrpc, xmlwriter, xsl, zip, mhash, ionCube Loader
GD: Yes
ImageMagick: Yes
PHP max upload size: 128M
PHP max form post size: 8M
PHP memory limit: 128M
Total max uploadable file size: 8M
Resource folder writeable: Yes
Session timeout: 24 minutes
Native JSON: Yes
Filter functions: Yes
Transliteration functions: No
PATH: /sbin:/usr/sbin:/bin:/usr/bin
PP_CUSTOM_PHP_INI: /var/www/vhosts/system/energypr.thirdfloordigital.net/etc/php.ini
PP_CUSTOM_PHP_CGI_INDEX: fastcgi
SCRIPT_NAME: /cms/core/settings/diagnostics/index.php
REQUEST_URI: /cms/core/settings/diagnostics/?extended
QUERY_STRING: extended
REQUEST_METHOD: GET
SERVER_PROTOCOL: HTTP/1.0
GATEWAY_INTERFACE: CGI/1.1
REMOTE_PORT: 34590
SCRIPT_FILENAME: /var/www/vhosts/thirdfloordigital.net/energypr.thirdfloordigital.net/cms/core/settings/diagnostics/index.php
SERVER_ADMIN: jamesh@thirdfloordesign.co.uk
DOCUMENT_ROOT: /var/www/vhosts/thirdfloordigital.net/energypr.thirdfloordigital.net
REMOTE_ADDR: 82.35.54.156
SERVER_PORT: 80
SERVER_ADDR: 205.186.165.244
SERVER_NAME: energypr.thirdfloordigital.net
SERVER_SOFTWARE: Apache
SERVER_SIGNATURE: <address>Apache Server at energypr.thirdfloordigital.net Port 80</address>
HTTP_COOKIE: _ga=GA1.2.434134852.1491830562; __atuvc=0%7C11%2C0%7C12%2C0%7C13%2C0%7C14%2C10%7C15; cmsa=1; PHPSESSID=i3qftg764hiiiv7gtmuui9due1
HTTP_ACCEPT_LANGUAGE: en-US,en;q=0.8,la;q=0.6
HTTP_ACCEPT_ENCODING: gzip, deflate, sdch
HTTP_REFERER: https://energypr.thirdfloordigital.net/cms/core/settings/diagnostics/
HTTP_ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
HTTP_USER_AGENT: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
HTTP_UPGRADE_INSECURE_REQUESTS: 1
HTTP_CONNECTION: close
HTTP_X_ACCEL_INTERNAL: /internal-nginx-static-location
HTTP_X_FORWARDED_FOR: 82.35.54.156
HTTP_X_REAL_IP: 82.35.54.156
HTTP_HOST: energypr.thirdfloordigital.net
UNIQUE_ID: WOyCGc26pfQAAHqp4rcAAAAD
FCGI_ROLE: RESPONDER
PHP_SELF: /cms/core/settings/diagnostics/index.php
REQUEST_TIME_FLOAT: 1491894809.0369
REQUEST_TIME: 1491894809

Thanks, K

Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_blog_date_archive_months() doesn't take any options. It's a horrible, crufty old function.

Would it be better to use something like this?

<?php perch_blog_custom([
       'template' => '_archive.html',
    'section' => 'blog',
    'filter' => 'visible',
]);?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

If that gets you the results you need then, yes, that would be better.

Can you use multiple templates with perch_blog_custom? I've tried combing the two templates (one for year and one for months) and it doesn't output/format correctly.

<perch:before>
<ul>
</perch:before>
    <li><a href="archive.php?year=<perch:blog id="year" />"><perch:blog id="year" encode="false" /> (<perch:blog id="year_qty" />)</a>
    <perch:blog id="months" encode="false" />
    </li>
<perch:after>
</ul>
</perch:after>

<perch:before>
<ul>
</perch:before>
    <li class="month"><a href="archive.php?year=<perch:blog id="postDateTime" format="Y" />&amp;month=<perch:blog id="postDateTime" format="m" />"><perch:blog id="postDateTime" format="%B" /> (<perch:blog id="month_qty" />)</a></li>
<perch:after>
</ul>
</perch:after>

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can do an if/different on the date, but not on two different formats of the date.

Does the design you're working to require both?

The reason I have the date formats: format="Y" and format="m" was for the slug.

In the design the format should be:

2016 (7) December (3) November (2) October (2)

Where I need the quantity of the posts and the year. But it outputs like this:

() December () () December () () December () () November () () November () () October () () October ()

Drew McLellan

Drew McLellan 2638 points
Perch Support

Hmm, yes, I see what you mean.

I don't think we have a good solution for this at the moment.

No worries drew, I did manage to filter it with <perch:if different /> but have left the quantity out for now as that didn't work with perch_blog_custom();

Thank you for your time!