• aronjeney

    (@aronjeney)


    Hi all, bare with me! I am trying to get or “print” keys (or values?) from a specific meta_box (the “field” name, id, options, etc.). The theme I am using has individual meta box files for each post type. For example: post-meta.php, page-meta.php, directory-meta.php, etc.), each contain their own functions and callbacks within each file.

    Heres what I have used in the past:

    print_r($meta_box['fields'][1]['name']);' 
    
    That will print "Header Image" from my following meta box code:

    $meta_box = array(
    ‘id’ => ‘metabox-directory’,
    ‘title’ => __(‘Custom settings for “Directory”‘),
    ‘description’ => __(‘Fill in this listing\’s details below..’),
    ‘post_type’ => ‘directory’,
    ‘context’ => ‘normal’,
    ‘priority’ => ‘high’,
    ‘fields’ => array(
    array(
    ‘name’ => __(‘Header Image’),
    ‘desc’ => __(‘The image should be no less than 1400-2000px wide’),
    ‘id’ => ‘header_bg’,
    ‘type’ => ‘file’,
    ‘std’ => ”
    ),
    )
    );`

    <strong><em>How would I adapt that code to print the values or “keys” (not sure of terminology) from specific meta box functions (see below):</em></strong>

    <?php
    add_action('add_meta_boxes', 'metabox_directory');
    function metabox_directory(){
    
    	$meta_box = array(
    		'id' => 'metabox-directory',
    		'title' => __('Custom settings for "Directory"'),
    		'description' => __('Fill in this listing\'s details below..'),
    		'post_type' => 'directory',
    		'context' => 'normal',
    		'priority' => 'high',
    		'fields' => array(
    			array(
    					'name' => __('Header Image'),
    					'desc' => __('The image should be no less than 1400-2000px wide'),
    					'id' => 'header_bg',
    					'type' => 'file',
    					'std' => ''
    				),
    			array(
    					'name' => __('Listing Address'),
    					'desc' => __('What is this listing\'s address?'),
    					'id' => 'directory_address',
    					'type' => 'text',
    					'std' => ''
    				)
    		)
    		);
    
    	$callback = create_function( '$post,$meta_box', 'create_meta_box( $post, $meta_box["args"] );' );
    	add_meta_box( $meta_box['id'], $meta_box['title'], $callback, $meta_box['post_type'], $meta_box['context'], $meta_box['priority'], $meta_box );
    }
    
    ?>

    I am trying to do all this in order to determine how to filter posts based on their meta-data..

    Any and all help/suggestions is truly appreciated – clearly I am a bit of a noob with this stuff but trying to learn! Thanks in advance!!

Viewing 3 replies - 1 through 3 (of 3 total)
  • lisa

    (@contentiskey)

    what end result are you looking for?
    are you using custom fields?
    which theme are you using?

    Thread Starter aronjeney

    (@aronjeney)

    I am actually trying to filter posts based on their meta data. Again, I’ve been able to do this in the past, however the meta data was all in one file vs individual meta files as I listed above. I believe it is the individual functions within each meta file that are getting in the way because if I comment the functions out temporarily I am able to print and display the data in my filters. I am not sure if I need to call the functions somewhere else somehow or add to my filter query or how I go about that. See below for more details..

    Here is my filter code (how I’ve done it in the past) with the “print” included to test:

    <ul id="filters">
    		<li><span>Sort by:</span></li>
    		<li>
    		    <?php
    	            $id = $_GET['directory_category'];
    	            print_r($meta_box['add_meta_boxes']['metabox_directory']['fields'][4]['options']);
    	        ?>
    
    	        <select id="sortCategory">
    	            <option value="" type="hidden">Category</option>
    	            <?php
                        foreach($meta_box['fields'][4]['options'] as $directoryKey => $directoryValue)
                        {
                            ?>
                            <option value="<?php echo add_query_arg(array('directory_category' => $directoryKey)); ?>"><?php echo($directoryValue);?></option>
                            <?php
                        }
                    ?>
    	        </select>
    	    </li>
    	</ul>

    And again, I think the function and/or callback is my culprit (top and bottom lines):

    <?php
    add_action('add_meta_boxes', 'metabox_directory');
    function metabox_directory(){
    
    	$meta_box = array(
    		'id' => 'metabox-directory',
    		'title' => __('Custom settings for "Directory"'),
    		'description' => __('Fill in this listing\'s details below..'),
    		'post_type' => 'directory',
    		'context' => 'normal',
    		'priority' => 'high',
    		'fields' => array(
    			array(
    					'name' => __('Header Image'),
    					'desc' => __('The image should be no less than 1400-2000px wide'),
    					'id' => 'header_bg',
    					'type' => 'file',
    					'std' => ''
    				),
    			array(
    					'name' => __('Listing Address'),
    					'desc' => __('What is this listing\'s address?'),
    					'id' => 'directory_address',
    					'type' => 'text',
    					'std' => ''
    				)
    		)
    		);
    
    	$callback = create_function( '$post,$meta_box', 'create_meta_box( $post, $meta_box["args"] );' );
    	add_meta_box( $meta_box['id'], $meta_box['title'], $callback, $meta_box['post_type'], $meta_box['context'], $meta_box['priority'], $meta_box );
    }
    
    ?>

    Again, do I need to call the functions and callbacks elsewhere somehow maybe? Thanks again for all and any help!!

    Thread Starter aronjeney

    (@aronjeney)

    Anybody? ?? I’m still bangin my head on my desk over here. Do I need to add an additional global call? Maybe something like “global $callback;”?

    Again, of course really appreciate all and any help. Thanks all!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get/Print keys/values from specific meta_box (custom fields)’ is closed to new replies.