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
Related
I'm trying to integrate a Query Expansion software with Wordpress, but the wordpress search engine is an AND searcher (returns those contents that contain all the words in your query, and therefore the more extensive is the query the fewer results returns).
For that reason when i use the query with the expansion features, the Searcher returns me less results.
I would like to modify the Search's behavior, and turn it into something more like an OR searcher.
Example:
For the query " iot, internet of things", I would want those posts which contents "iot", or "internet of things, or both of them.
I've reading through this document (https://codex.wordpress.org/Class_Reference/WP_Query) and i tried to use something like this:
add_action( 'template_redirect', 'hooks_setup' , 20 );
function hooks_setup() {
if (! is_home() ) //<= you can also use any conditional tag here
return;
add_action( '__before_loop' , 'set_my_query' );
add_action( '__after_loop' , 'set_my_query', 100 );
}
function set_my_query() {
global $wp_query, $wp_the_query;
switch ( current_filter() ) {
case '__before_loop':
//replace the current query by a custom query
//Note : the initial query is stored in another global named $wp_the_query
$wp_query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
//others parameters...
) );
break;
default:
//back to the initial WP query stored in $wp_the_query
$wp_query = $wp_the_query;
break;
}
}
But I don't know which parameters i need to set in wp_query. How can I achieve this?
To answer what parameters you need to set, you have to read through the same page you mentioned. Parameters for various items (post types or categories) are defined on this page and when you want to change it all you have to do is modify the parameters.
If you dont know what query you want to do I think its hard for people to help. If you are getting less results, you can open query a bit further or obtain results in 2 queries. Read through the page and you will find its common when people want to obtain different types of data and not everything is in the loop.
Also read through this to understand more about the loop and how to reset the query and do a new one then you can obtain two result sets and use them or combine them when and where you want.
Just to give you an example for string search in posts with s as parameter from the same page Prepending a term with a hyphen will exclude posts matching that term. Eg, 'pillow -sofa' will return posts containing 'pillow' but not 'sofa' (available since Version 4.4).
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 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.
My client has a custom post type called 'Clients'. She uses this to record all information about each client she has. She then sometimes leaves these in either draft mode or published. She would like to be able to see all of her clients at a glance and copy and paste all of their details straight from the web page on a regular basis. I have created the following query (note the query is only showing fist and last name at the moment for testing):
<?php
$args = array(
'post_type' => 'clients',
'post_status' => array('draft', 'publish'),
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$fname = get_field('fname');
$sname = get_field('sname');
echo '<p>'.$fname.', '.$sname.'</p>';
endwhile;
?>
However, this returns no results. When I change the 'posts_per_page' to a number, the query works. However, if this number is above 58 no results are returned again. I think somewhere there is a limit to how much can be output in 1 query. I cannot use pagination at the minute as the client is admit she'd want to see all information on one page. Is there another way of querying this without limits? Or a way to adjust the limit?
Thank you in advance
Without being able to debug it directly I'm not sure why you're seeing this issue. Alternatively you can try using the 'nopaging' => true option instead of the 'posts_per_page' option. Does that method behave?
Are you sure that the custom post type name is clients and not client? That is the only thing that I can point, as I the only parameter that is custom by the developer ^^.
The problem was down to my local setup, as soon as it was uploaded to the clients server the error disappeared. Sorry I forgot to close the question
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.