$wp_query and WP_QUERY - same arguments, different results - wordpress

I spend this whole day trying to figure out a problem with a combination of a custom query and custom post types. This is my last resort...
The setting
I wrote a plugin that introduces some custom post types to my WordPress. To display them in the main query I hooked them into the query like this:
function add_cpt_to_query( $query ) {
*some code...*
// add custom post types to query
if ( !( is_admin() || is_post_type_archive() || is_page() ) && $query->is_main_query() ) {
$query->set('post_type', array_merge( array('post'), $cpt ) );
}
}
add_action('pre_get_posts','add_cpt_to_query');
In my theme on the other hand I setup ajax pagination like this:
function setup_pagination() {
global $wp_query;
$max_pages = $wp_query->max_num_pages;
$current_page = ( $wp_query->paged > 1 ) ? $wp_query->paged : 1;
$ajaxurl = admin_url( 'admin-ajax.php' );
wp_register_script( 'ajax-pagination', get_template_directory_uri() .'/js/dummy.js', array('jquery'), '', true);
wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
'max_pages' => $max_pages,
'current_page' => $current_page,
'ajaxurl' => $ajaxurl,
'query_vars' => $wp_query->query_vars
));
wp_enqueue_script( 'ajax-pagination' );
}
add_action( 'wp_enqueue_scripts', 'setup_pagination' );
function pagination() {
$query = $_POST['query_vars'];
$query['paged'] = $_POST['next_page'];
/*
$query = array(
'paged' => 2,
'post_type' => array('post', 'custom_post_type_1', 'custom_post_type_2' )
);
*/
$posts = new WP_Query( $query );
$GLOBALS['wp_query'] = $posts;
// Start the loop.
while ( have_posts() ) : the_post();
?>
*some code...*
<?php endwhile;
die();
}
add_action( 'wp_ajax_nopriv_ajax_pagination', 'pagination' );
add_action( 'wp_ajax_ajax_pagination', 'pagination' );
and the script part:
$.ajax({
url: ajaxpagination.ajaxurl,
type: 'post',
data: {
action: 'ajax_pagination',
query_vars: ajaxpagination.query_vars,
next_page: parseInt(ajaxpagination.current_page) + 1
}
});
The problem
If I pass the query_vars array I get from $wp_query with the altered 'paged' value back to WP_QUERY, it returns the wrong set of posts. It looks like, that WP_QUERY does not account for the cpts in the loop. Though these cpts are mentioned in the 'post_type' of the query_vars array and thereby passed along to the new WP_QUERY.
When I manually set 'post_type' and only pass this argument, it works as intended. The aspect that blows my mind is, that the resulting query_vars used in the ajax call to WP_QUERY are exactly the same, but only when I manually set 'post_type' the pagination works as it should.
I dont know if this was a somewhat understandable explanation, but I'm thankful for every idea that could help me out. Big THX!

Ok... I got it now.
I made a mistake in wp_localize_script(). The query_vars should be a json-string, I on the other hand just passed the array itself. My code above has to be altered in two lines:
function mk_setup_pagination() {
...
wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
...
'query_vars' => json_encode($wp_query->query_vars) <- convert to json-string
));
...
}
function mk_pagination() {
$query = json_decode( stripslashes( $_POST['query_vars'] ) , true); <- convert json-string to array
...
Works like a charm now. :)
btw: the code is based on a tutorial by wpmudev.org: Loading WordPress Posts Dynamically With AJAX

Related

Why wc_get_template_part() not working inside wc_get_products()?

Cant find full loop example on the web with wc_get_template_part() and wc_get_products(), so looking for help here:
global $woocommerce;
global $product;
$args = array(
'limit' => 15,
'category' => array('printers', 'laptop')
);
$query_cats = wc_get_products($args);
foreach ($query_cats as $query_cat) {
echo $query_cat->get_id();
echo $query_cat->get_title();
// echo "<pre>";
// var_dump($query_cat);
wc_get_template_part('content', 'product');
}
?>
Titles and ids are displayed, var_dump also, bu wc_get_template_part - no. I have add_theme_support('woocommerce'); and also body_class();
WooCommerce content-product.php template only works only with standard loop(with instance of Wp_Query). May be following solution can help:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => [
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => ['printers', 'laptop'],
)
],
);
$product = new WP_Query( $args );
while ( $product->have_posts() ) {
$product->the_post();
wc_get_template_part( 'content', 'product' );
}
wp_reset_postdata();
Thanks
Yes, it can be done (2022 update)
If you want to avoid native WP_Query or using shortcodes, this actually can be done using wc_get_products().
You just need to setup and reset postdata within your foreach loop and setup WooCommerce loop properly.
global $post; // Do not forget this!
$args = array(
'limit' => 15,
'category' => array('printers', 'laptop')
);
$products = wc_get_products( $args );
// Set loop properties
wc_set_loop_prop('columns', 5);
// Start custom WC loop
woocommerce_product_loop_start();
foreach( $products as $product ) {
// Setup postdata
$post = get_post( $product->get_id() );
setup_postdata( $post );
// Get template part
wc_get_template_part( 'content', 'product' );
}
// End loop and reset postdata
woocommerce_product_loop_end();
wp_reset_postdata();
Important note: this will only work if queried products are within any HTML element with the woocommerce class (otherwise WooCommerce CSS won't load for your products). In some templates, the woocommerce class is already part of the DOM (e.g. <body> element), but if it isn't, wrap your loop within an element as such:
<div class="woocommerce my-products-loop">
<?php // Your loop goes here ?>
</div>
TIP: You can set various loop properties using wc_set_loop_prop in the "Set loop properties" part of the code

Custom WP_Query for search

I'm trying to make a custom WP_Query for a search results page.
With the following code, the page always displayed all posts regardless:
<?php
$args = array(
'posts_per_page' => '-1',
);
$query = new WP_Query( $args );
?>
So I've added the search Query to the $args, but this always returns no results - where is this going wrong?
<?php
$search_query = get_search_query();
echo $search_query;
$args = array(
'posts_per_page' => '-1',
's' => $search_query
);
$query = new WP_Query( $args );
?>
1) You can use the templatesearch.php and searchform.php as your starting points. Creating a Search Page Codex
2) As far as the custom query goes, you can use pre_get_posts hook to test if you're on a search page, then you get $_GET your values, edit your query accordingly. Action Reference - pre_get_posts
There are tons of tutorials online and questions on this exchange to help you out. Some are Simple and others are more Complex. You'll have to do some real research to accomplish this. Hope it helps!
I have solved this using pre_get_posts - I'm still intrigued as to why using the WP_Query method doesn't work, but here's what I'm now using:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('posts_per_page', '-1');
}
}
}
add_action('pre_get_posts','search_filter');

Exclude children of custom post type from search

I have a custom post type of 'location'. I then have children pages for each of the pages for that cpt. so it looks something like this, "www.example.com/location/location-name/child-page/", each child page is using a post template of "location-product-services.php". So what I am trying to do is exclude from the search results the children of this cpt.
I am trying to do it by checking the meta data to see if it is using that template. I just cant seem to get it working. Any help would be great.
This is what I currently have -
// Exclude local product and services pages from search result.
function location_subpages_exclude_search( $query ) {
if ( is_search() && !is_admin()) {
$query->set('meta_query',
array(
'key' => '_wp_page_template',
'value' => 'location-product-services.php',
'compare' => '!='
)
);
}
}
add_action('pre_get_posts', 'location_subpages_exclude_search');
Thanks in advance.
First, I pretty much exclusively use the Relevanssi plugin any time I want to modify search. But to search programmatically, I think this is what you're after:
$taxonomy = 'location';
$terms = get_terms($taxonomy, array( 'parent' => 0, 'search' => $query ) );
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
<?php endif;?>
Use the function get_terms to search your CPT, the 'search' is your $query (you might consider wrapping the search string with the SQL wildcard '%') and 'parent'=>0 returns only the top level.
I figured it out.
First I got all parent pages of my post type, used get_pages() grab them all.
Looped through each of the parent pages and ran another get_pages() for children of that parent.
function SearchFilter($query) {
if ($query->is_search) {
$args = array(
'hierarchical' => 0,
'post_type' => 'location',
'parent' => 0, //returns all top level pages
'post_per_page' => -1
);
$parents = get_pages($args);
$excludes = array();
foreach($parents as $parent) :
$args = array(
'post_type' => 'location',
'child_of' => $parent->ID,
'post_per_page' => -1
);
$children = get_pages($args);
foreach($children as $child):
array_push($excludes, $child->ID);
endforeach;
endforeach;
$query->set('post__not_in', $excludes);
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

Wordpress: Custom post type segregation on Category page

I've got two custom post types, for example 'Cars' and 'Bikes'. I've used Wordpress's default category to categorise the posts from the two post types. Let's say for example the categories are 'Red', 'Blue' and 'Black'.
What I'm trying to achieve here is that when I go to the category page for 'Red', I want to see the 'Cars' and the 'Bikes' that are categorised under 'Red'. I'm using the category.php and this is the query that I'm trying to run:
$car_args = array(
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'cars',
'post_status' => 'publish',
'cat' => $cat
);
// The Query
$car_query = new WP_Query( $car_args );
// The Loop
if ( $car_query ->have_posts() ) {
echo "<h3>Cars</h3>";
while ( $car_query->have_posts() ) {
$car_query->the_post();
echo get_post_type() . '' . get_the_title() . '<br />';
}
} else {
// no posts found
}
$bikes_args = array(
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'bikes',
'post_status' => 'publish',
'cat' => $cat
);
// The Query
$bikes_query = new WP_Query( $bikes_args );
// The Loop
if ( $bikes_query ->have_posts() ) {
echo "<h3>Bikes</h3>";
while ( $bikes_query->have_posts() ) {
$bikes_query->the_post();
echo get_post_type() . '' . get_the_title() . '<br />';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
The $cat in the query gets the category id of 'Red' category. Both these queries are correctly restricting the posts by the 'Red' category but posts from both the 'Cars' post type and 'Bikes' post type are showing up even though I've tried to restrict by post type. I've read in a few articles that Wordpress ignores the post type args on the category page. Is this true and if it is, is there a workaround for this?
What you are trying to do is possible with one query only, and only with the main query without any custom queries.
First of all, lets first add your custom post types to your category page. By default, custom pist types are excluded from category pages. So we need to add this manually to the main query arguments via pre_get_posts. Add the following to your functions.php: (CAVEAT: Untested and also requires PHP 5.3+)
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_category() ) {
$q->set( 'post_type', array( 'post', 'cars', 'bikes' ) ); // Change this according to your post types
$q->set( 'nopaging', true ); // This will get all posts, same as posts_per_page=-1
}
});
You should now have all posts from a clicked category is your set post types on your category pages.
Next, wee need to sort out your loops. Delete all your custom queries in category.php and replace it with the default loop. As you would want two block ordered by post type, we will use rewind_posts() so we can run the loop twice
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
if ( $post->post_type == 'cars' ) { //Change accordingly to only show cars in this loop
// Your loop
}
}
rewind_posts();
while ( have_posts() ) {
the_post();
if ( $post->post_type == 'bikes' ) { // Change accordingly to only show bikes
// Your loop
}
}
}
This should now display your posts in two block sorted by post type

How to refresh pagination links after submit new query?

I modified WordPress main loop for blog posts, made it query and loop through different types of posts according to what filter is submitted.
At the beginning of the HTML, there is a form with several buttons as option, it's similar to the SA site menu: Questions Tags Users Badges Unanswered.
When visitor click one of the menu button, a new WP_Query is submitted.
Below the options, is the loop of posts, depends on what filter is submitted.
The php codes is basically a standard wordpress loop with customized functions:
$args= !empty($_POST['filter']) ? $_POST['filter']:null;
get_option_nav();
if( my_loop_have_posts(array('filter'=>$args))):
while my_loop_posts(): the_my_loop_post();
get_template_part('contents');
endwhile;
endif;
Things are working fine except that only the default filter get the correct pagination links. After submit a new filter, when click the pagination links, it goes to the default filter's pages. How to modify wordpress pagination links to get it work for the other filters?
Here's the code that I use for query args:
function get_args($args){
$defaults= array(
'order' => 'DESC',
'orderby' => 'modified',
'max_num_pages' =>5,
'paged' => get_paged (),
'post_status' => 'any',
'post_type' => array ('post', 'docs','topics'),
'posts_per_page' => 5,
)
$args = wp_parse_args ( $args, $defaults );
extract ( $args );
if(!empty($args['filter'])){
switch ($args['filter']){
case 'top_voted':
$args['post_type'] = 'docs';
$args['meta_key'] = '_vote_total';
$args['order'] = 'DESC';
$args['orderby']='meta_value_num';
break;
case 'unanswered_questions':
$args['post_type] = 'topics';
$args['blabla'] = 'blabla';
break;
default:
blabla;
break;
}
return $args;
}
and the code for the paged:
function get_paged() {
global $wp_query;
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( ! empty( $wp_query->query['paged'] ) ) {
$paged = $wp_query->query['paged'];
}
if ( ! empty( $paged ) )
return (int) $paged;
return 1;
}
When you make a custom query, you need to explicitly call every parameter of the query. Thus, standard ones disappear, as is the pagination case.
Depending on how you are making this new query you could try:
To concatenate at the beginning $query_string, for example
$query = new WP_Query( $query_string . 'order=DESC' ) As it is explained in the Codex:
If you want to preserve the original query parameter information that
was used to generate the current query, and then add or over-ride some
parameters, you can use the $query_string global variable in the call
to query_posts().
To get the pagination from the default query and use it in your custom query
$paged = get_query_var('paged'); $query = new WP_Query( array( 'paged' => $paged ) );
In your case there is something wrong with your get_paged() function. I have tested it and it does not work, but I have not found the bug. Instead, I tried what I usually use, and it works for me:
function get_paged() {
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { $paged = get_query_var('page');
} else { $paged = 1; }
return $paged;
}
Something seems to fail in you else if statement. Let me know if this solves your issue.

Resources