你如何按文章ID过滤Joomla 3文章

时间:2014-12-19 13:52:24

标签: joomla3.0 joomla-module

我正在尝试创建一个基于类别和标签显示文章的Joomla模块。

我已经从https://github.com/lasinducharith/joomla-tags-selected中选择了标记文章的代码,并将其引入mod_articles_news的副本中:

public static function getList(&$params)
{
    $app = JFactory::getApplication();
    $db         = JFactory::getDbo();

    // Code base on https://github.com/lasinducharith/joomla-tags-selected to fetch ids of tagged articles 
    $tagsHelper = new JHelperTags;
    $tagIds = $params->get('tagid', array());
    $tagIds = implode(',', $tagIds);
    echo '<pre>search for tags[' . $tagIds . ']';

    $query=$tagsHelper->getTagItemsQuery($tagIds, $typesr = null, $includeChildren = false, $orderByOption = 'c.core_title', $orderDir = 'ASC',$anyOrAll = true, $languageFilter = 'all', $stateFilter = '0,1');

    $db->setQuery($query, 0, $maximum);
    $results = $db->loadObjectList();

    $article_ids=array();   
    foreach ($results as $result){
        $article_ids[]=$result->content_item_id;
    }
    echo ' yields articles[' . implode(',', $article_ids ) . ']</pre>';

    // Get an instance of the generic articles model
    $model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));

    // Set application parameters in model
    $appParams = JFactory::getApplication()->getParams();
    $model->setState('params', $appParams);

    // Set the filters based on the module params
    $model->setState('list.start', 0);
    $model->setState('list.limit', (int) $params->get('count', 15));

    $model->setState('filter.published', 1);

    $model->setState('list.select', 'a.fulltext, a.id, a.title, a.alias, a.introtext, a.state, a.catid, a.created, a.created_by, a.created_by_alias,' .
        ' a.modified, a.modified_by, a.publish_up, a.publish_down, a.images, a.urls, a.attribs, a.metadata, a.metakey, a.metadesc, a.access,' .
        ' a.hits, a.featured' );

    // Access filter
    $access     = !JComponentHelper::getParams('com_content')->get('show_noauth');
    $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
    $model->setState('filter.access', $access);

    // Category filter
    $model->setState('filter.category_id', $params->get('catid', array()));

    // Article filter
    $model->setState('filter.id', $article_ids);

    // Filter by language
    $model->setState('filter.language', $app->getLanguageFilter());

    // Set ordering
    $ordering = $params->get('ordering', 'a.publish_up');
    $model->setState('list.ordering', $ordering);

    if (trim($ordering) == 'rand()')
    {
        $model->setState('list.direction', '');
    }
    else
    {
        $direction = $params->get('direction', 1) ? 'DESC' : 'ASC';
        $model->setState('list.direction', $direction);
    }

    // Retrieve Content
    $items = $model->getItems();
    $newitems=array();

    foreach ($items as &$item)
    {
        echo '<pre>fetched article[' . $item->id .'] which is ';

        if ( !in_array($item->id, $article_ids )){
            echo 'not tagged';
        } else{
            echo 'tagged';

        }
        echo '</pre>';
        ...
    }
    return $newitems;
}

调试语句给我:

  • 搜索标签[2]产生文章[40,44,45]
  • 获取标记为
  • 的文章[45]
  • 获取标记为
  • 的文章[44]
  • 获取未标记的文章[43]
  • 获取未标记的文章[42]
  • 获取未标记的文章[41]
  • 获取标记为
  • 的文章[40]
  • 获取未标记的文章[39]
  • 获取未标记的文章[38]

所以文章还没有被id过滤。我会非常感谢任何建议。 感谢

1 个答案:

答案 0 :(得分:1)

发现问题,过滤器应该是article_id:

    // Article filter
    $model->setState('filter.article_id', $article_ids);

现在它根据标签和类别选择文章