Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return all values stored in var outside foreach loop

So I assume something is being overwritten but I am unsure as to how to stop this and retrieve all values outside loop. Any ideas?

foreach($gallids as $gallterm)
{
    $postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));

    $checkmissing = $postterms[0];                  
    print_r($checkmissing); // Check Terms for missing images - works here.
}

print_r($checkmissing); // Check Terms for missing images - not working here 
                        // - seems to be getting last or first val only.
like image 971
user1134561 Avatar asked Nov 18 '25 06:11

user1134561


2 Answers

First of all initialize the variable you want to use later on:

$checkmissing = array();

Then inside the foreach append the first entry of the post terms to that array:

foreach($gallids as $gallterm)
{
    list($checkmissing[]) = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));
}

See $checkmissing[], that is what effectively will prevent that you overwrite it. It will append each to the array.

Finally you can output the result after the loop:

print_r($checkmissing);

Note: You should do some additional handling if wp_get_post_terms returns an empty array:

foreach($gallids as $gallterm)
{
    $terms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"))
        AND list($checkmissing[]) = $terms
    ;
}
like image 159
hakre Avatar answered Nov 19 '25 23:11

hakre


I tried a few of the examples above and they didn't quite work the way I wanted. So I poked around and did a little research, here's how I got it working for me.

In my particular case I needed to grab all the category ID's for a specific post, then return that variable into the WP_Query arguments array.

First, you will need to grab the post terms

$terms = get_the_terms( $post->ID, 'category' );

Next you'll want to initialize the variable you want to use later on:

$cat_terms = array();

Next you'll declare the foreach to get each individual term ID

foreach ( $terms as $term ) {
    $cat_terms[] = $term->term_id;
}

Now let's say you want to use return a comma separated list for this $cat_terms variable. We're going to use the 'join' function

$comma_separated_terms = join( ", ", $cat_terms );

Now let's say you want to use this variable to put into you WP_Query loop for say the 'category__in' parameter. We're going to use 'array_values'.

$values = array_values($cat_terms);

The nice thing about this is now we can insert this $values variable into the WP_Query arguments:

<?php global $post;
        $query = new WP_Query(array(
            'post_type' => 'post_type_name',
            'category__in' => $values));
?>

In my particular case, the client wanted some custom post types to display in the sidebar based on the blog posts categories. So I needed to get all the blog post terms and match them up with the terms for the custom post types categories.

Final Code Looked something like this:

<?php
    $terms = get_the_terms( $post->ID, 'category' );

    $cat_terms = array();

    foreach ( $terms as $term ) {
        $cat_terms[] = $term->term_id;
    }

    $values = array_values($cat_terms);
?>
    <h3><?php echo $title; ?></h3>

    <?php global $post;
        $query = new WP_Query(array(
            'post_type' => 'custom_post_type',
            'category__in' => $values));

            if ( $query->have_posts() ) : ?>

                <?php while ( $query->have_posts() ) : $query->the_post(); ?>

                // Code for loop goes here

                <?php endwhile; endif; ?>

            <?php wp_reset_postdata(); ?> 
like image 21
John Avatar answered Nov 19 '25 23:11

John



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!