Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get Meta Value from taxonomy field in Meta Box Deluxe WordPress

  • SOLVED

Trying use the Meta Box Deluxe http://metabox.io/docs/get-meta-value/ to show a list of the categories using the checkbox_list and pull posts with the selected categories in a loop. meta_value is the ID for the field. If I echo $meta_value it just shows array, array, array, but not the selected categories. Hope that makes sense.

Here's how my query looks:


<?php
$meta_value = get_post_meta($post->ID, 'gt_meta_value', FALSE);

$i = 1;
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}

query_posts(
array(
'showposts' => '-1',
'cat' => implode(",", $meta_value),
'paged' => $paged ) );
if ( have_posts() ) : $count = 0; while ( have_posts() ) : the_post(); $count++;
?>

Answers (4)

2015-01-05

Farid answers:

Hi,

You can add the taxonomy meta box filed to your post something like this:



array(
'name' => __( 'Categories', 'textdomain' ),
'desc' => __('Post Categories', 'textdomain'),
'id' => "gt_meta_value",
'type' => 'taxonomy',
'options' => array(
'taxonomy' => 'category',
'type' => 'checkbox_list'
),
),


Now you can get selected categories in posts loop in this way:



$categories = rwmb_meta( 'gt_meta_value', 'type=taxonomy&taxonomy=category' );

foreach ( $categories as $category ){
$cat_ids[] = $category->term_id;
}



$i = 1;

if ( get_query_var('paged') ) {

$paged = get_query_var('paged');

} elseif ( get_query_var('page') ) {

$paged = get_query_var('page');

} else {

$paged = 1;

}


query_posts(

array(

'showposts' => '-1',

'cat' => implode(",", $cat_ids),

'paged' => $paged ) );

if ( have_posts() ) : $count = 0; while ( have_posts() ) : the_post(); $count++;


Justin Young comments:

This worked. Thanks so much!

2015-01-04

Arnav Joy answers:

try this

$meta_value = rwmb_meta( 'gt_meta_value', 'type=checkbox_list' );

query_posts(

array(

'posts_per_page' => '-1',

'cat' => implode(",", $meta_value),

'paged' => $paged ) );


Justin Young comments:

Didn't work. I thought that would work as well, but not the case.


Justin Young comments:

Sorry, can't give these details out because it's a client and I don't have permission.


Arnav Joy comments:

can you change this line

$meta_value = get_post_meta($post->ID, 'gt_meta_value', FALSE);

to this

$meta_value = get_post_meta(get_the_ID(), 'gt_meta_value', FALSE);

or this

$meta_value = get_post_meta(get_the_ID(), 'gt_meta_value', TRUE);


Justin Young comments:

Those didn't work. I had also tried those.


Arnav Joy comments:

try this

$meta_value = rwmb_meta( 'gt_meta_value', 'type=checkbox_list' ,get_the_ID());


Justin Young comments:

Still nothing.

Thanks for your efforts.

2015-01-04

Dbranes answers:

Hi, what's the output of:

var_dump( $meta_value );

for your trials?

You shouldn't use <em>query_posts()</em>, the [[LINK href="http://codex.wordpress.org/Function_Reference/query_posts"]]following note is from the Codex[[/LINK]]:

<blockquote>Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose.</blockquote>


Justin Young comments:

Thanks, I'll read through the codex on that.

The var_dump didn't yield any results.


Dbranes comments:

You can use <em>WP_Query()</em> or <em>get_posts()</em> or the <em>pre_get_posts</em> action.

<blockquote> If I echo $meta_value it just shows array, array, array,</blockquote>

Did you use the <em>var_dump( $meta_value );</em> in that case?

If <em>echo</em> shows <em>array</em>, then <em>var_dump( $meta_value );</em> or <em>print_r( $meta_value );</em> will show you what's inside that array.

2015-01-05

Romel Apuya answers:

is this a template?? can you show the full code?
you might need to declare

global $post;

at top..