Push metadata in array wordpress - php

Applying meta keys in search result
Since I need to take on a different approach to get my data in javascript from wordpress, I am trying to apply (as follow up on my post earlier today (https://stackoverflow.com/questions/34873986/javascript-for-each-custom-post-type-wordpress)) metadata to my query so it will spit out the right information. This is practically harder than I thought, cause I preferly want it in one query. I been googling now for quite a while, cause I had hoped I wasn't the only one with this problem. I cant seem to find a relevant answer to my problem
Case
Since I was being pointed to the fact javascript and php dont play nice, I was beginning to explore a different way. It requires me to get the results on forehand before I push this into javascript via a json_encode. So far this is running fine, except for one tiny thing. When asking in WP_query I cant get any meta data, which is the part I really need, since that contain map information for my google maps. So is there a way I can query that before I push this through?
<?php
$args = array(
'post_type' => 'hotels',
);
$query = new WP_Query( $args );
echo '<pre>';
print_r($query->posts);
echo '</pre>'
?>
<script type="text/javascript">
var post_info = <?php echo json_encode($query->posts); ?>;
<script>
So far I managed to check what is been given, but I sume, I cant get this via wp_query. Is there another way to actually get the data I need? Or is there a way I can push the meta data in the array with each single post, so I can acces it in Javascript?

Are you looking for get_post_meta() ? https://developer.wordpress.org/reference/functions/get_post_meta/
But you will have to call get_post_meta on each post :/
EDIT :
<?php
foreach($query->posts as $post) {
$post->meta = get_post_meta($post->ID);
}
?>

Related

Passing MULTIPLE vars with add_rewrite_endpoint fails

I have read many tutorials about passing multiple variables to a post/page in wordpress, using add_rewrite_endpoint. I am currently passing 2 variables, calendar_year / calendar_month . It works good for both but separately each one. Using both like (http://myserver/mysite/availability/calendar_year/2016/calendar_month/10/) only gets the year and if I pass them on reverse, only gets the month. Availability page shows correct (No 404 error), but it fails taking the last parameter when there are the two of them. Here is my code for add_rewrite_endpoint
function booking_calendar_add_endpoints() {
add_rewrite_endpoint('calendar_year', EP_ALL);
add_rewrite_endpoint('calendar_month', EP_ALL);
}
add_action('init','booking_calendar_add_endpoints');
And this is how I get it and use it:
global $wp_query;
$year_var = $wp_query->get( 'calendar_year' );
$month_var = $wp_query->get( 'calendar_month' );
I am sure I am missing something. Does anyone have a solution for it? Thanks.

WP_Query 500 internal error / Scope between php files

OK - here's the problem: I perform a new WP_Query on one php file:
$args = array( 'post_type' => ... );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
This gives me a set of all relevant posts. I then do some hierarchical logic stuff and I ask the user to make a selection which I then send back to the server (using _POST).
In a second php file where I receive the user selection, I basically need to access the same set of posts. However, when I try to an identical WP_Query I get a 500 internal server error... I am probably missing something really simple here. Any ideas why I cannot perform another query? And/or as a workaround, is it possible to still access the query results from the first file (e.g. by saving the info I need into some kind of global multidimensional array?) if that's even possible
Excuse my ignorance, first time web developer... Thanks

How can I get an id from a simplepie post which can be used to look it up later?

I've recently started developing a portfolio website which I would like to link to my wordpress blog using simplepie. It's been quite a smooth process so far - loading names and descriptions of posts, and linking them to the full post was quite easy. However, I would like the option to render the posts in my own website as well. Getting the full content of a given post is simple, but what I would like to do is provide a list of recent posts which link to a php page on my portfolio website that takes a GET variable of some sort to identify the post, so that I can render the full content there.
That's where I've run into problems - there doesn't seem to be any way to look up a post according to a specific id or name or similar. Is there any way I can pull some unique identifier from a post object on one page, then pass the identifier to another page and look up the specific post there? If that's impossible, is there any way for me to simply pass the entire post object, or temporarily store it somewhere so it can be used by the other page?
Thank you for your time.
I stumbled across your question looking for something else about simplepie. But I do work with an identifier while using simplepie. So this seems to be the answer to your question:
My getFeedPosts-function in PHP looks like this:
public function getFeedPosts($numberPosts = null) {
$feed = new SimplePie(); // default options
$feed->set_feed_url('http://yourname.blogspot.com'); // Set the feed
$feed->enable_cache(true); /* Enable caching */
$feed->set_cache_duration(1800); /* seconds to cache the feed */
$feed->init(); // Run SimplePie.
$feed->handle_content_type();
$allFeeds = array();
$number = $numberPosts>0 ? $numberPosts : 0;
foreach ($feed->get_items(0, $number) as $item) {
$singleFeed = array(
'author'=>$item->get_author(),
'categories'=>$item->get_categories(),
'copyright'=>$item->get_copyright(),
'content'=>$item->get_content(),
'date'=>$item->get_date("d.m.Y H:i"),
'description'=>$item->get_description(),
'id'=>$item->get_id(),
'latitude'=>$item->get_latitude(),
'longitude'=>$item->get_longitude(),
'permalink'=>$item->get_permalink(),
'title'=>$item->get_title()
);
array_push($allFeeds, $singleFeed);
}
$feed = null;
return json_encode($allFeeds);
}
As you can see, I build a associative array and return it as JSON what makes it really easy using jQuery and ajax (in my case) on the client side.
The 'id' is a unique identifier of every post in my blog. So this is the key to identify the same post also in another function/on another page. You just have to iterate the posts and compare this id. As far as I can see, there is no get_item($ID)-function. There is an get_item($key)-function but it is also just taking out a specific post from the list of all posts by the array-position (which is nearly the same way I suggest).

Wordpress Ordering Issues

I've got a theme that I'm using and it's throwing posts up on a slider on my home page but I would like to order the way they show up. The code currently in place is:
<?php
//OptionTree Stuff
if ( function_exists( 'get_option_tree') ) {
$theme_options = get_option('option_tree');
$homeCategory = get_option_tree('home_category',$theme_options);
$homeNumber = get_option_tree('home_number',$theme_options);
}
?>
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('cat='. $homeCategory .'&showposts='. $homeNumber .''.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
And the way I would like this to be ordered would be the following:
orderby=meta_value&meta_key=event_date&order=ASC
I've never used option tree before in obtaining information from some theme options, so I'm kind of confused on how I would integrate that ordering method with the other code. Any help is much appreciated, thanks!
Jarth
I would need to dig through the documentation for the option tree, but I definitely recommend retrieving the values into variables before you plug them into the query. For example, fist dump the meta value into a variable. Then retrieve the values you want from that array... and so on. At any point you can do a var_dump() to make sure you're getting the expected values.
Once you have that working, you should be able "orderby" the appropriate column.

Getting Post Information Outside the Wordpress Loop

I know there are functions to like is_single() that will return data about the page, but I'm looking for a way to get the following information outside of the loop:
The category of the single post.
AND
The title of the single post.
All I would really need is the post ID in question and I could get all the other information. I've looked through the functions reference in the codex, but I haven't found anything. Is this impossible because the script doesn't even get that information 'til the Loop runs?
(I would need this information in both the header and footer, so before and after the PHP script for the loop, if that is a problem.)
Hopefully someone can offer some insight.
EDIT: To clarify: I want the information to be from the post that is loaded in the loop on the "single" page. (AKA the post they are viewing.) So how would I get this ID in the first place? Basically, when viewing a post, I want to get its category or title, but not while the loop is going.
This is actually quite simple.
// Gets an array of post objects.
// If this is the single post page (single.php template), this should be an
// array of length 1.
$posts = get_posts();
// The post object contains most/all of the data that is accessible through the
// normal functions like `the_ID()`, `the_author()`, etc
var_dump($posts[0]->ID);
This can be performed outside of and it doesn't affect the normal loop. For example, my single.php template looks like this (line numbers included):
1. <?php
2. get_header();
3. $posts = get_posts();
4. var_dump($posts[0]->ID);
5. ?>
6. <div id="post">
You could execute your own query and return a $post object and then access it's attributes through something like
echo $post->title;
Read about $wpdb and Custom Queries on the Codex. Basically you could run a SQL query using the ID as a where filter through its $wpdb object.
Another option, more WP-like, would be to use a Custom Query Post, where you could define another loop, returning only your post using its id:
query_posts('p=2010'); // 2010 being the post's ID
Then you could run the alternative loop and use the similar $post object to display some information.
This is what I use inside the loop to run an new query; I just tried it outside the loop and it works, on a single.php template page. This will give the title of the latest post in mycategory. I don't know how to pull the category ID, though.
$my_query = new WP_Query('category_name=mycategory&showposts=1');
while($my_query->have_posts()):
$my_query->the_post();
the_title();
endwhile;
After the query has run, try running:
global $wp_query;
var_dump( $wp_query );
The information you are looking for is probably in $wp_query->query_vars or $wp_query->post
You can use the $wp_query global object, and address all returned posts like this
$wp_query->posts ; // contains an array of posts.
Then, if for instance you need to know if the first post is of a certain post_type, you can do this:
$first_post_type = $wp_query->posts[0]->post_type;
Couldn't you store the $post information in a separate variable on the page, then just echo the data you need out later when you need it (ie, outside the loop)?
This is predicated on needing the data after the initial loop has run.
The usual thing is to create "second loop" which would extract needed post, hence your needed data.

Categories