0

I am using this code for a meta_query

  $values = shortcode_atts( array('category'  => 'Test',), $atts );
   $args = array(
    'post_type' => 'mytype',
    'post_status' => 'publish',
    'meta_query' => array(
        array(
          'key'     => 'br_type',
          'value'   => esc_attr($values['category']),
          'compare' => '='
        ),
    ),
  );
   $query = new WP_Query( $args );

How could I assign the result of br_type to a php variable?

2
  • what exactly do you mean of assigning the result of br_type? Your code shows that you have assigned a value to it. Did you mean you would like to result of the query instead? Or you would like to assign the value and save it to the br_type meta key? May 16, 2020 at 0:41
  • Do you mean: How can I assign the result of the br_type meta query to a variable? Your question doesn't make much sense to me either. May 19, 2020 at 16:32

3 Answers 3

0

Sorry if I'm being dumb but your query says that br_type must be equal to esc_attr($values['category']) so to put it in a variable you would just:

$br_type = esc_attr($values['category']);

Or if you want to get it from $query you would look in one of the places mentioned here:

How do I get a meta value from WP_Query?

0

In your $args array you are defining what content you want to show with WP_Query. You are saving your new query to the $query variable.

So now you are able to loop through the posts that fit your defined arguments:

<?php

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    echo '<ul>';
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

If you want to save the value of "br_type" you will need an array, because for every post there is a value for this meta field.

You can create an empty array and a counter variable and loop through your posts. On every loop your are saving the value of your meta field inside the array at an index position.

$br_type_values = []; // empty array
$counter = 0; // counter variable for array

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $your_post = get_the_ID();
        $br_type_values[$counter] = get_post_meta( $your_post , 'br_type', true );
        $counter++;
    }
} else {
    // no posts found
}

This way you have all the values of "br_type" inside a php variable.

0

I'm not 100% sure what you mean, but you could loop through the posts and add the br_type meta value to an array:

// your query code here

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $br_type[] = get_post_meta(get_the_ID(), 'br_type');
    }
}

wp_reset_postdata();

Then $br_type should be an array with all the values of br_type from your posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.