Passing an array from a loop in one partial to another partial
-
I can’t figure out how to do this to save my life.
In my index.php file, before the loop, I created a global variable,
global $index_of_ids
Once the loop starts, I put all the post id’s from the loop, into it.
$index_of_ids = $post->ID;
I also wrote a plugin for a sidebar widget that uses wp_query, and I want to exclude the posts in the global $index_of_ids from it (just using the wp_query arguments).
But the plugin cannot recognize that the global variable exists!
How can I access it from the plugin?
Thank you
-
If the variable should contain all of the IDs in the loop then it should be an array line this:
$index_of_ids[] = $post->ID;
Using this:
$index_of_ids = $post->ID;
will only store the ID of the last post.Sorry that was a typo. It is a proper array like $blah[] .
After the loop in index php, I use print_r to spit back out the values in the array for debugging, and the array is definitely getting written correctly.
The plugin with the wp_query loop just can’t access it. When I try to invoke $index_of_posts[] in the plugin, it just throws a php error and tells me it’s not an array. (because it’s obviously not seeing that it is declared from index.php)
because it’s obviously not seeing that it is declared from index.php
Then did you re-declare it on the plugin file?
global $index_of_ids;
Something like this works for me:
global $index_of_ids; $the_query = new WP_Query( array( 'post__not_in' => $index_of_ids ) ); if ( $the_query->have_posts() ) { echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' . get_the_title() . '</li>'; } echo '</ul>'; } wp_reset_postdata();
Yeah.
My index.php has this:
<?php global $index_post_list; //Create global array for index id's?> <?php while (have_posts()) : the_post(); ?> <?php $index_post_list[] = $post->ID; //put index posts into global array ?> ...the rest of the loop here
Then after the loop ends: I have this:
<?php print_r(array_values($index_post_list)); //PRINT THE ARRAY ?>
just to make sure it’s being written correctly. It is.
Now the plugin works properly. If I leave wp_query without any arguments, it outputs a “recent posts” list, just as you would expect. But then I made this change to the plugin’s loop:
global $index_of_ids; $custom_query = new WP_Query( array( 'post__not_in' => $index_of_ids ) ); if ( $custom_query->have_posts() ): while ( $custom_query->have_posts() ) : ..print out each post, etc.. endwhile; else: ..print a no post message, etc.. wp_reset_query();
and it still just outputs the recent posts.
So then I try to figure out what the deal is, by adding this immediately after the plugin’s loop:
<?php print_r(array_values($index_post_list)); ?>
and the result is this like this:
The print_r after the index.php loop says:
Array ( [0] => 432 [1] => 359 [2] => 332 [3] => 322 [4] => 313 )
Which is great! I thought, but then upon closer look:
[0] => 432 [1] => 359 [2] => 332 [3] => 322 [4] => 313
it looks like [2] and [3] have the same post id. But the same post does NOT repeat in the index.
But no worries, at least I know the array is getting “SOME” kind of id’s written to it. Maybe that’s a mistake in my index.php file? (can’t find it yet).
But the real problem is the print_r immediately after the plugin’s loop. It simply prints an error message:
Warning: array_values() expects parameter 1 to be array, null given in /...blah..blah...blahh.../wp-content/plugins/FeaturedPosts/FeaturedPosts.php on line 92
So now I am really confused. ??
Any help is greatly appreciated.
Ok, so new information.
It turns out, my index loop DOES have 2 repeating post id’s. But the 2 articles are different. I don’t know what to think of this. I am using permalinks, so I just go into the admin sections post’s and check the url of the post I am editing, and the URL to edit BOTH of these DIFFERENT posts is:
https://www.mywebsite.com/wp-admin/post.php?post=332&action=edit
No idea what is going on here ??
I did import some some posts from a previous version of the site, hopefully that caused that problem. I deleted the offending post, but the plugin still produces the same error.
it looks like [2] and [3] have the same post id.
They’re not the same. One is 332 and the other is 322.
But the real problem is the print_r immediately after the plugin’s loop. It simply prints an error message:
Why print
$index_post_list
again? Guessing from your code above, you never changed the value. Or is there some relevant code left out on your previous reply?What exactly is the problem? You have the theme’s loop show the most recent posts. Then you have a plugin that shows the same thing minus the posts that are already shown. Is this not happening in your case?
Ah, you’re right about the 2 posts. I must be going crazy.
Thanks for replying.
I printed the $index_post_list again ( from the plugin’s php file ) just to see what was stored in it, so I would know if the array was reachable by the plugin.
If I don’t print the array, I see no error. But the posts from the main loop are NOT excluded from the plugin’s sidebar widget loop. I guess the array is not an array, so it doesn’t work as an exclusion argument on wp_query.
I’ve only got about 20 posts in the database, while I build the site. I can just F5 refresh while sitting on index.php and see duplicate posts, so I know it isn’t working.
It’s probably just a typo. Your reply above mentions the index.php using
$index_post_list
to store the IDs. But on your plugin you use$index_of_ids
which is a different variable.I removed all the work I did on this already. So now I have a clean slate. I’ll try it again and be extra cautious. I hope you’re right.
@somedude77 I think the typo error @ronangelo pointed out might be the issue with your code. I did put up a sample plugin to try and replicate your issue with the codes above , but I did not find any. Also in my sample plugin, the result for the query is displayed via a shortcode, and I did insert the shortcode directly inside the index.php file.
However, inserting the shortcode before the loop in the index.php file, the global $index_of_ids is not available as the loop has not been executed yet, but using the shortcode after the loop in the index.php file, the global is available, I do not know whether this is the issue. Just double check and report your results.
Thanks. I’m just getting started for the day, so I will try to make sure I have no errors this time and give it another shot.
But now I am thinking about your reply. There are 2 components to this code. One being the index.php and it’s contained loop. The other being my plugin which resides in the sidebar. Now, in my index.php I call the “sidebar BEFORE the index loop. Will PhP try to run the sidebar first, thus, referencing the global variable before it is created, and then run the index loop?
If this is the case, then it explains why the variable is working in index.php but not in the plugin.
Oh my god, that was it. So firstly, thank you both for replying so promptly. I do really appreciate it.
moving the get_template_part(‘…’); AFTER the loop in index.php solves the problem.
That’s no good. It’s critical to my design that it be before the area with the loop. ??
Is there any work around to this?
Let’s see a screenshot of your design layout before and after moving the get_template_part(‘…’).
Well, the problem is this. The sidebar with the widget is on the right. The main loop is on the left.
I have 2 divs inside a container. The left one has a margin-right equal to the width of the right one. Then the right one is floated right. This achieves a sidebar with a fixed width, but a re-sizable content area.
Now this is a problem because the only way I know to get the same effect, AND change the order of the html, is to do something like this:
https://jsfiddle.net/Lezvx63d/
But I simply cannot use calc() since legacy IE browsers are too important for me. (also no jquery, flexbox, etc.. I can basically use html, css, and JS only if it’s not essential to the usability of the site)
Is there any other way to achieve this effect?
- The topic ‘Passing an array from a loop in one partial to another partial’ is closed to new replies.