Recently added items based on subject

If I wanted to add a function that pulls out and displays recently added items based on a specific subject - what’s the easiest way to do that? Is there a shortcut or is this another plugin?

Is it possible to modify the recently added items function? where would one find it?

It wouldn’t be a new plugin, but almost certainly a function in your theme’s functions.php file. It would be a modified version of the get_recent_items() and recent_items() functions in application/libraries/globals.php that basically combines the two.

get_recent_items() does the essential work of querying the database. Your version would do basically the same as that function, but add in the query parameters for an advanced search on subject.

1 Like

Thanks - this of course makes a lot of sense. Here’s what I’m trying to do: we have long introductory texts that are currently in Simple Pages. What we want to do is pull out recent items from the database related to specific subjects – we will likely designate certain items as “featured” as well. If I understand you correctly, I just basically modify the functions.php to include a new function, e.g.

function get_random_featured_items_intro($num = 5, $hasImage = null)
{
    return get_records('Item', array('featured' => 1,
                                     'sort_field' => 'random',
                                     'hasImage' => $hasImage), $num);
}

And modify the array to include the search on subject category.
Would I then be able to include that in the simple pages or do I need to make it into a shortcode first?

That’s about right. The array to include the subject search will be kinda big, but not too bad.

The plan to put it in Simple Pages changes a part of my original response, though. All the above works to do this as a theming function, but I’m not 100% sure that that’ll work for this situation, where it sounds like there are multiple simple pages, each of which would look for a different subject.

If that’s accurate, you probably would have to make a plugin to – as you suggest – would make a shortcode to use in those pages. Once you are in the world of writing a plugin putting the stuff in functions.php doesn’t quite make sense: best to put all the same code in the plugin directly.

1 Like

Correct, multiple simple pages each based on the subject. The function / shortcode would need to pull out recently added or featured items for just that subject. So it does seem like shortcode is the best way to go. Hopefully I’ll get some development time early next week to do this.

Just following up here, I made a very very simple plug-in for this. When I get a chance I guess I could clean up the code, add some features, and package it as a plugin (never done that before! total newbie with github…) but in the meantime here’s the code that works for the main function, in case anyone else needs it:

public function subject_shortcode($args, $view)
    {
   
   if (isset($args['name'])) {
            
       $subject = $args['name'];
 
if (isset($args['num'])) {
            $limit = $args['num'];
        } 
        
        else {
            $limit = 5; 
        }

 $items = get_records('Item', array('advanced' => 
            array(
                array(
                    'element_id' => 49,
                    'type' => 'is exactly',
                    'terms' => $subject
                )),
            'sort_field' => 'added',
            'sort_dir' => 'd'
            ),$limit);
       $html = '';
        foreach ($items as $item) {
           $html .= $view->partial('items/single_intro.php', array('item' => $item));
        }
      return $html;
     }
}


So to use the shortcode would just be: [subject name=“subject name”] and you can obviously set a value for “num” to pull a certain number of records. It displays most recent items first by default; I suppose that could be made into an option instead if I want to package this for others.

I can’t believe it took me so long to finally do this… it was not complicated at all. Thanks to @patrickmj and @jflatnes for patience.

This topic was automatically closed after 250 days. New replies are no longer allowed.