clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

In Wordpress get post metadata when using wpalchemy class

Filed under: WordPress— Tagged with: custom fields, meta box, metadata, php

Getting a post metadata values, when using the helpful wpalchemy class, differs a bit from the normal method.

You may get acquainted to the class here.

Get values on a single page

It's as simple as...:

echo $custom_metabox->get_the_value('custom_field_name');

If you have trouble echoing out the value, try calling the global variable before it.

global $custom_metabox;
echo $custom_metabox_info->get_the_value('custom_field_name');

Get values on an archive page

This is a bit different from how you get the values on a single page. Example use case, get buy links to the reviewed album.

global $custom_metabox;//This is possible needed

$custom_metabox->the_meta();//This is needed on an archive page
echo $custom_metabox_->get_the_value('custom_field_name');//Get it

Example use case

Record review thingy. This is on a single page. We get values, put then in variables and finally spew 'em out as nice list:


<?php
  $info_buy         = $custom_metabox_info->get_the_value('info_buy');
  $info_buy_digi    = $custom_metabox_info->get_the_value('info_buy_digi');
  $info_artist      = $custom_metabox_info->get_the_value('info_artist');
  $info_release     = $custom_metabox_info->get_the_value('info_release');
  $info_label       = $custom_metabox_info->get_the_value('info_label');
  $info_year        = $custom_metabox_info->get_the_value('info_year');
  $info_catalogue   = $custom_metabox_info->get_the_value('info_catalogue');
  $info_format      = $custom_metabox_info->get_the_value('info_format');

  echo '*   ' . $info_artist . '—' . $info_release . ' ' . $info_format . '
';
  echo '*   Released on <span class="dehighlight2">' . $info_label . '</span> with cat# <span class="dehighlight2">' . $info_catalogue . '</span> in <span class="dehighlight2">' . $info_year . '</span>
';
  if (!empty($info_buy)) {echo '[Get the 12" &rarr;]()';}
  if (!empty($info_buy_digi)) {echo '[Digital &rarr;]()';}
  echo '

Loop a set of fields

You know those where you can add more fields by clicking a plus sign.

//Start the loop
while($custom_metabox->have_fields('music_uploader')) {
  $my_music_file  = $custom_metabox->get_the_value('music_file');
  $my_music_title = $custom_metabox_food_music->get_the_value('music_file_title');
  if(empty($my_music_file)) {
    //Do something if empty
  } else {
    //Do something else if not empty
  }
}//End the loop

Comments would go here, but the commenting system isn’t ready yet, sorry. Tweet me @hiljaa if you want to make a correction etc.

  • © 2021 Antti Hiljá
  • About
  • Follow me in Twatter → @hiljaa
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • console.log('Smash the patriarchy!')
  • I love u!