Forum

Thread tagged as: Question, Discussion, Docs

Best way to return a single field?

Think I may have asked this before but... what is best way of fetching the value of a particular field with perch_content_custom?

I currently do as follows:

$articleData = perch_content_custom('Article', 
  array(
    'page'=>'/article.php',
    'skip-template'=>'true'
  )
);
$articleAuthor = $articleData[0]['article-author'];
Keir Moffatt

Keir Moffatt 0 points

  • 6 years ago

Ah, looks like I may already be doing it 'right':

Then $result[0] will be your item, and presumably something like $result[0]['category'] will be your categories.

From https://forum.grabaperch.com/forum/09-03-2014-portfolio-fetching-latest-item-related-items-from-same-category?page=1#reply-4405

Drew McLellan

Drew McLellan 2638 points
Perch Support

I'd like to provide a neater way to do this for quick-hit fields. What's stopping me is:

  1. Devising a syntax that is flexible enough but also non-verbose, and

  2. Making sure that mis-use (as inevitably will happen) doesn't have negative impacts on page performance

Thanks for that insight.

When my mind was wandering, an additional parameter sprang to mind, e.g. 'field'=>'article-author' ...but then what happens if you want multiple fields? Perhaps you then wander into 'field'=>array('article-author','article-date') territory... and it suddenly doesn't feel any neater.

On second look, it strikes me that it is possibly as neat as it is going to get - I guess the only thing would be skipping the [0] parent array if it only returns a single item... but, again, probably not really worth it.

shrug