• Have this code to display $unique_thelocation(meta value) as Tags and display each post foreach tag but its not displaying well. Not sure if my code is right.

    but i need help to display each post $unique_thelocation. Thank you!

    $place = get_category(get_query_var('cat'))->name;
    	echo '<div>';
    	query_posts( array ( 'category_name' => $place, 'posts_per_page' => 5 ) );
    	while (have_posts()) : the_post();
    
    		$args = array('location'=>'' );
    		$place = get_post_meta( get_the_ID(), 'place', true );
    		$place = wp_parse_args($place, $args);
    		foreach($place as $key => $value)
    		{
    			$thelocations[] = $place[location];
    		}
    
    	endwhile;
    	$unique_thelocations = array_unique($thelocations, SORT_REGULAR);
    	foreach($unique_thelocations as $unique_thelocation)
    	{
    		echo '
    <li><a>' . $unique_thelocation . '</a>' ';
    	  			if (have_posts() ) :
    				while (have_posts()) : the_post();
    					echo '<p>';  the_title();   '</p>';
    				endwhile;
    			endif;
    		echo'</li>
    ';
    	}
    	echo '</div>';
Viewing 3 replies - 1 through 3 (of 3 total)
  • First, your initial foreach doesn’t make sense, you’re doing foreach with a key value pair, but not using either key or value…

    Problem 2, the location key, well… isn’t a key, nor is it surrounded by quotes, so your code should break right there.

    Instead of me just pointing everything out in a sentence, have a look at your current code, commented:

    https://gist.github.com/JayWood/b071afd337c9e2bf1086/9991dba9a2daf625e64e3981db5aa79abf9f8a22

    Now the clean version of the same gist, heavily commented:

    https://gist.github.com/b071afd337c9e2bf1086

    Let me know if this helps you, I’d be glad to explain further.

    Thread Starter silvermonkey

    (@silvermonkey)

    @jerry thank you for the comments and suggestions..but still the code return empty.

    The ‘location’ is an array value of the Meta_key ‘_map’. basically the first loop pulls all the value[location] (serialize) and then, should display all post related to that value ‘location’. Not sure if i explain it well but thank you so much for the help at least the code you gave is clear.

    Unfortunately I can’t really see what you see ( I don’t have your database ). So, if line 46 is empty ( on the last gist ) then something is wrong.

    I would start dumping data to your error log and watch it to see where it’s failing, you can do this very simply by enabling WP_DEBUG which you should if you’re developing anyhow.

    Then just start dropping error_log calls like so:

    error_log( print_r( $var, true ) );

    Swapping $var for the variable you’re logging of course. I would probably do this at minimum on lines 32, 35, and 39.

    Line 32 I’d log $location_data and lines 35 & 39 I’d log it as well, though with some sort of identifier as to which if statement your in, something like this:

    error_log( 'IF TRUE' . print_r( $var, true ) );

    I could have just misunderstood how the location_data is grabbed or how it looks. Or, as I like to say, I’m not perfect, and could’ve botched it somewhere along the line, but only a debug log can tell.

    Not to put a shameless plug here, but I wrote a pretty extensive article on debugging in WordPress, might be worth a read: https://webdevstudios.com/2015/10/15/debugging-wordpress-tips-snippets/

    Unfortunately I don’t know what your data looks like, so I’m kind of limited in what I can help you with, but I hope this at least points you in the right direction.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get post Foreach tag displays mutilple post in invalid array’ is closed to new replies.