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.
Related
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);
}
?>
I am not sure how to describe my request, which means that I have had a hard time Googling it. Anyway what I am looking for is the following:
Lets say I have a archive with 15 posts. The archive page is restricted to 10 posts per page. Now I would like to show on the first page post 1-10 and on the second page post 11-15. I tried to google it but with no luck. All I did find was a general wp_query post count:
<?php
echo $wp_query->post_count;
?>
I guess it shouldn't be too hard to accomplish correct? Anyone who can help me out here?
Thanks
the thing that you wanted is so easy,
wordpress query do this default for you,
install wp-pagenavi plugin its show a pagination non the footer and only thing you need to do is set the post limits on 10 in wordpress general setting section.
if this is not what you wanted, so please explain with an example.
********** i found out what you mean of this ***********
first you have to get posts_per_page , you can get that value whit usin one of these codes
$default_posts_per_page = get_option( 'posts_per_page' );
or
global $wp_query;
$total_posts = $wp_query->post_count;
after that you need to know witch page your in , so you can use this code to get that
$current = intval(get_query_var('paged'));
now we need a simple calculate to make the numbers you needed,
<?php
$ppp = intval(get_option( 'posts_per_page' ));
$current = intval(get_query_var('paged'));
$last = $ppp * $current;
echo 'post from : ' . ( ($last+1) - $ppp). ' to : '.$last;
?>
I'm pretty new to PHP so perhaps lacking some basics so here goes.
For WordPress I've a function for replacing some text from a TablePress table. The function worked fine when I used code along these lines:
function replace_stuff($text) {
if (is_front_page() || is_page('2611') || is_child('2611')) {
$replace_magic = array(
//text to search => text to replace
'dog' => 'cat',
'mouse' => 'elephant'
);
}
$text = str_replace(array_keys( (array)$replace_magic), $replace_magic, $text);
return $text;
}
add_filter('tablepress_table_output', 'replace_stuff');
So in that example dog would be displayed on the frontend as cat & mouse as elephant.
But now I would like to complicate things & create the strings to replace by querying fields from all posts in a custom post type "drivers".
I have come up with something like this, with the aim of finding any text that matches the post title & replacing with text from a custom field (of all posts from my 'drivers' custom post type), but it doesn't do anything!
function replace_stuff($text) {
if (is_front_page() || is_page('2611') || is_child('2611') || get_post_type() == 'drivers') {
query_posts('post_type=drivers');
if (have_posts()) :
while (have_posts()) : the_post();
$profilenat = get_post_meta($post->ID, 'driver_nationality', true);
$profiletitle = get_the_title();
$replace_magic = array(
//text to search => text to replace
$profiletitle => $profilenat
);
endwhile;
endif;
}
$text = str_replace(array_keys( (array)$replace_magic), $replace_magic, $text);
return $text;
}
add_filter('tablepress_table_output', 'replace_stuff');
Could anyone advise me please?
Many thanks.
Firstly I think you need to replace
$replace_magic = array(
with
$replace_magic[$profiletitle] = $profilenat
Currently, if everything else works, then for every driver you are completely replacing the contents of $replace_magic with a new array, which just has that driver's details. Instead you want to add a new item to the existing array.
Going further, with this sort of problem it can be really useful to do some quick debugging to help you narrow down where the problem might be. So here it would be useful to know if the problem is really with your str_replace, or if it's actually with the code above it.
Debugging in Wordpress is worth a read, and having done that you can use error_log to output some details to debug.log inside your wp-content directory.
Before your str_replace, doing
error_log(print_r($replace_magic));
Would tell you if your query loop has worked as you intended or not.
If it hasn't, you might then put a log statement inside the loop. This will tell you if the loop contents are being executed at all (in which case the problem is with your code inside the loop), or not (in which case the problem may be with your query).
Additionally, if you haven't already I would recommend checking the WordPress Codex on query_posts. query_posts manipulates the main Wordpress query, and may give you some really unexpected results used inside a filter like this. At least consider WP_Query - and check the note about wp_reset_posts.
Hope that helps - apologies if some of it is stuff you already considered, but as you mentioned you're quite new to PHP I hope it's useful.
I’m having trouble displaying variations in a custom built template, each time i call the wpsc function wpsc_have_variation_groups() within my loop i get the following php error
commerce/wpsc-includes/product-template.php on line 1419 [22-Nov-2012 23:27:39] PHP Fatal error: Call to a member function have_variation_groups() on a non-object in /home/tofapost/public_html/sandbox/wp/wp-content/plugins/wp-e-commerce/wpsc-includes/product-template.php on line 1419.
wpsc_have_variation_groups() is being called inside a WP_Query loop like so;
$args = array('post_type' => 'wpsc-product', 'posts_per_page' => -1);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
...
<?php if (wpsc_have_variation_groups()) { ?>
<?php } ?>
...
endwhile;
Whats strange is other wpsc functions like, wpsc_the_product_id() and wpsc_product_has_stock() work while no functions related to variations do...
Any help appreciated
Thanks
This has already been answered. The issue with this question is the fact that not all the code is displayed and the wrong type of loop was used. to list the products.
The answer that was used was to manually obtain the variations because the type of loop does not allow for the ID to be used as there was no identifier for the variations that needed to be obtained. To be able to use the current code it needed to use a different loop so or change it so that the variation is manually obtained. In this case the variation was manually obtained.
global $wpsc_variations;
$wpsc_variations = new wpsc_variations( get_the_ID() );
Reference: https://wordpress.stackexchange.com/questions/73689/issue-displaying-variations-in-custom-template-using-wpec-3-8-9-2
The wpsc_the_product() function sets up a global $wpsc_variations object that the product variation functions use.
I think in order to use wpsc_the_product() you need your query to be the global $wp_query, but you can setup $wpsc_variations yourself after you open your loop:
while ($loop->have_posts()) : $loop->the_post();
global $wpsc_variations;
$wpsc_variations = new wpsc_variations( get_the_ID() );
That hopefully should get all the product variation functions working.
You can refer to the following location :
https://wordpress.stackexchange.com/questions/73689/issue-displaying-variations-in-custom-template-using-wpec-3-8-9-2
I think this may help you to resolve your problem.
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.