Custom wordpress query not working - php

I have the following code in the "archive.php" file (from my current active theme):
list($f_categ_name) = explode('/', get_category_parents($cat));
if ( $f_categ_name == "X_CATEG") {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'category_name' => single_cat_title( '', false ),
'meta_key' => 'x_categ_type',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => $paged,
);
// get results
$the_query = new WP_Query( $args ); // The Loop
?>
<?php if( $the_query->have_posts() ): ?>
I have 2 categories : A (with 15 posts) and B (with 7 posts) and they have the same parent category(let's say X_CATEG). So basically I run the same code but $the_query->have_posts() returns true for A category and false for B category. WHY?
Using $GLOBALS['wp_query']->request I was able to debug the executed query and both queries (for category A and B) returned all post IDs.

In the WP_Query arguments, category_name will be category slug Not Category title.
single_cat_title functions will return the page title if a category or tag archive is queried. see reference
So You should use category slug in WP_Query arguments instead of category title.
Hope this will help.

Related

Get page parameter from woocommerce product url

I have woocommerce set up in a site and it has an ajax switch. With this the view of products changes from list to grid and vice versa. In the ajax call, I have queried the products again through a custom query using this code
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 20,
'product_cat' => $_GET['category'],
'paged' => $paged
);
$count = 0;
$loop = new WP_Query( $args );
But I dont get the required output from get_query_var(10). The url of the product page is as follows.
https:example.com/product-category/producers/arturo/page/2/?view=grid
Through this url all I need is the page parameter with value 2. How can I fetch it in the ajax call.

Custom post types displaying all category posts instead of showing posts only from specific category

I am having issues with custom post type categories display.
I have created custom post type for review site
I want to show different categories in different tabs
but when I put any category of review in menu it shows all reviews
instead of showing reviews from specific category
For example:
I have created 2 categories in reviews
a) Games
b) software
Whenever I choose Games category it shows posts from software category too.
I had same issue with blog posts categories but I resolved that issue using code
in my category.php file
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$cat_id = get_cat_ID( single_cat_title(null, false) );
query_posts(array(
'post_type' => 'post',
'paged' => $paged,
'cat'=>$cat_id,
));
I have created taxonomy.php file for custom post type
<?php $mypost = array( 'post_type' => 'cpreviews','paged' => $paged);
$loop = new WP_Query( $mypost ); ?>
Can anyone please help us to understand what we need to do to display posts
according to categories for custom post types?
UPDATED CODE IN TAXONOMY.PHP but still have some issue:
I have changed above code under taxonomy.php to
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//$currentTerm = $_GET[ 'term' ];
$cat_id = get_cat_ID( single_cat_title(null, false) );
$mypost = array('post_type' => 'cptreviews',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'product_reviews_product_category',
'terms' => (''),
'field' => 'slug',
'name' =>'Product Category',
)
)
);
$loop = new WP_Query( $mypost ); ?>
Now whenever I put category in 'terms' => ('kids') like this it shows all posts under that category only. but I want to take that 'terms value' dynamically.
Try this one:
<?php
$type = 'cpreviews';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Supposing you have Custom Post Type: cpReviews --- Custom Taxonomy: RevCategories --- Create new review post and chose category from RevCategories. Query cpReviews will definitely show all the posts, you need to do some thing like this -----
query_posts(array(
'post_type' =>'cpreviews', //Custom_Post_TYpe
'showposts' => $limit,
'RevCategories' => 'Games',)); //Custom Post Type TAxonomy (can use page name here get_query_var('pagename'); for dynamic content
while (have_posts()): the_post(); global $post; echo the_title(); endwhile;
I have resolved this issue by creating taxonomy-{taxonomy}.php file & removed tax query code..it automatically takes given category..thanks all for your help
This will resolve this issue.
$args = array(
'post_type'=> 'post',
'cat' => 'Games'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

Exclude all sub categories from wp_query

I've been searching high and low for and answer to this, but I'm not actually sure it's possible!
I have a WP_Query that pulls posts from almost everything, however, I wish to exclude a specific category and/or all it's sub categories.
Searching around people are yet to find a solution for this.
Here's my query so far:
$args = array(
'post_type' => 'sell_media_item',
'cat' => -98,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
I thought just excluding cat 98 would grab all the sub categories too, but apparently not.
I've tried using:
category__not_in, depth=0, parent=0 and even an adaptation of this, with no luck.
Any ideas?
[EDIT]
I'm using a custom taxonomy called Collections, so putting 'collection' => 'vip' into the query means it will only show this collection. I'm thinking if there's a way of reversing this so it excludes the collection instead?
As it's not possible to list all of the categories that will appear here as they will be changing all of the time.
[EDIT 2]
After the discussion in the comments below, here's the updated code.
$ex = array(
'taxonomy' => 'collection',
'child_of' => 98,
'hide_empty' => 0
);
$categories = get_categories($ex);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
echo('<pre>'); var_dump($categories);
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php echo('<br /><pre>'); var_dump($args); ?>
<?php $loop = new WP_Query( $args ); ?>
I would get the list of all sub categories with get_categories() and then build a 'cat' exclusion array based on the results.
$args = array('parent' => 98);
$categories = get_categories($args);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
This is just an example, you may have to modify it slightly to fit your needs.
So!
It appears I was trying to do the impossible. I couldn't get this script working for the life of me. So I tried a different angle. Instead of excluding a custom taxonomy and its terms, I decided to move all of my other terms into a parent term and just called that instead.
Here's the code if anyone's interested...
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'sell_media_item',
'taxonomy' => 'collection',
'term' => 'clubs',
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : while ($loop->have_posts()) : $loop->the_post(); ?>
I wrote my own function in order to exclude subcategory posts from the loop, using tips from the above post and elsewhere.
In my theme archive.php file, above the loop, I list the subcategories (optional):
<?php
$current_cat = get_queried_object();
$args = array( 'parent'=>$current_cat->term_id, 'child_of' => $current_cat->term_id, );
$categories = get_categories( $args );
foreach($categories as $category) { ?>
<h2><?php echo $category->name ;?></h2>
<p> etc....</p>
<?php } ?>
In my functions.php file, I've added the following custom function using pre_get_posts:
add_action( 'pre_get_posts', 'main_query_without_subcategory_posts' );
function main_query_without_subcategory_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
if ( is_category() ) {
//Get the current category
$current_category = get_queried_object();
//get the id of the current category
$current_cat_id = $current_category->term_id;
//find the children of current category
$cat_args = array( 'parent'=>$current_category->term_id, 'child_of' => $current_category->term_id, );
$subcategories = get_categories( $cat_args );
//Get a list of subcategory ids, stick a minus sign in front
$subcat_id = array();
foreach($subcategories as $subcategory) {
$subcat_id[] = " -". $subcategory->term_id;
}
//join them together as a string with a comma seperator
$excludesubcatlist = join(',', $subcat_id);
//If you have multiple parameters, use $query->set multiple times
$query->set( 'posts_per_page', '10' );
$query->set( 'cat', ''.$current_cat_id.','.$excludesubcatlist.'' );
}
}
}
Then in the archive.php, below the subcategories, I've added the regular WordPress loop which is now being modified by the above function:
<?php while (have_posts() ) : the_post(); ?>
<h2><?php the_title();?></h2>
<p> etc....</p>
<?php endwhile;?>
Though the WordPress codex says that using "category__in" will exclude posts from subcategories, that didn't work for me and subcategory posts were still showing.
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
https://developer.wordpress.org/reference/hooks/pre_get_posts/

WordPress: Custom Query

I have a custom query that uses an array merge so I can list articles and custom post type posts in one list, showing 5 at a time. Pagination below allows the user to see the rest of the results. I keep getting pagination errors, but I've tried virtually every variation of the $paged variable in my query to get pagination to work and it doesn't. I know it's me, and probably a simple syntax thing...but I'm stumped. Any ideas? (Note: this page has multiple, other custom queries above the one in question)
Here's my code:
<?php
$loop1 = array_merge( $wp_query->query,
array( 'post_type' => array('post','podcasts', 'cat' => $cat_ID ),
'paged' => ( get_query_var('page') ? get_query_var('page') : 1 ),
'posts_per_page' => 5 ) );
query_posts( $loop1 );
while (have_posts()) : the_post(); ?>
have you tried?
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),

Create a Wordpressloop with two posts of the next page

I need to create a wordpress-site which shows 5 posts on the front and in a different loop: 2 posts of the next page. These show up if you visit the second page. Do you have any ideas? I wanna show "More articles" by displaying "older" posts in a different loop.
Thanks
For the first page, limit your listing to 5 posts per page in wordpress settings.
Then you create the second page and apply a page template in witch you have made a custom query to fetch 2 posts.
$cat = get_cat_ID($category);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = 2; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'category__in' => array($cat),
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => $post_per_page,
'caller_get_posts' => $do_not_show_stickies
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
...
To read more, see Wordpress Codex Reference

Categories