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/
Related
I'm in the middle of a small crisis here. I need your help! I've done my research for hours but this is my last resort. Cutting to the story, I'm trying to list all the 'pages' from an active category in Wordpress.
Here are the steps I've done so far. I was able to enable categories for my pages by adding the following code to my functions.php file:
function jh_cats() {
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
add_action( 'init', 'jh_cats' );
Next, I needed to make it so that the website has categories & sub-categories. The sub-categories will only show on the category page of the parent. I've added the following to my category.php file to get the list of all my sub-categories.
<?php
$categories = get_categories( array(
'orderby' => 'name',
'parent' => $cat,
'hide_empty' => FALSE
) );
if ($categories)
{
foreach ( $categories as $category )
{?>
<li><?php echo $category->name ?></li>
<?php
}
} ?>
Now, when you visit a specific sub-category (considering nothing will be assigned to the main one), I need to be able to list all the pages that is assigned that sub-category.
What can I do next? I have this in my index.php but this just lists all the pages. Regardless if you're in a specific category or not.
<ul>
<?php
global $post;
$myposts = get_posts( array(
'post_type' => 'page',
'posts_per_page' => 5
) );
if ( $myposts ) {
foreach ( $myposts as $post ) :
setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
<?php the_content(); ?>
</li>
<?php
endforeach;
wp_reset_postdata();
}
?>
</ul>
Hope you can help! Thanks. :)
Credits to replies/code from others over here to help me solve some of the earlier issues.
On the page you wish to filter posts, you can try using $cat = get_the_category($post->id); to return array. Then use a foreach or just get the first item:
$cat_id = $cat[0]->cat_ID;
Then update the WP query to show all posts using that category.
By ID:
$query = new WP_Query( array(
'post_type' => 'page',
'cat' => $cat_id
) );
By name:
$cat_name = get_cat_name($cat_id);
$query = new WP_Query( array( 'category_name' => $cat_name ) );
Source: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Have you tried something like this, for your index.php code:
$myposts = get_posts( array(
'post_type' => 'page',
'taxonomy' => 'category'
) );
Also, I would try and avoid "get_categories", it's best to try and use the "get_terms" function whenever you can.
I need a bit help from you. I have a custom search engine to search products from a post type taxonomy :
if( isset($_POST['search_products'] ) {
/// codes ....
$_SESSION['ids'] = $my_ids;
$args = array(
'post_type' => 'product',
'showposts' => -1,
'post__in' => $_SESSION['ids']
)
$posts = new Wp_Query($args);
}
This query outputs about 60 products with pagination (10 products per page), but when user visits the page without using the search engine, all products should be displayed. Instead, the $_SESSION remains and display only the previous results.
I just want the pagination working when I do search, and all products displayed when I access the page without using the search engine.
Does any Wordpress expert have an idea ?
Thank you.
if( isset($_POST['search_products'] ) {
/// codes ....
$_SESSION['ids'] = $my_ids;
$args = array(
'post_type' => 'product',
'showposts' => -1,
'post__in' => $_SESSION['ids']
)
$posts = new Wp_Query($args);
}
else {
$args = array(
'post_type' => 'product',
'showposts' => -1,
)
$posts = new Wp_Query($args);
}
simply put an else block
CODE ,
global $post;
$id = intval($_GET['cat']);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$products = new WP_Query( array(
'post_type' => 'products',
'order' => 'ASC',
'posts_per_page' => 5,
'paged' => $paged
) );
endwhile;
HTML ,
<ul class="pagination pull-right">
<li><?php echo get_next_posts_link( 'Next Page', $products->max_num_pages ); ?></li>
<li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>
So on my template for taxonomy-product_tag.php, I want to get all product id's from the Category.
Here is how I currently do it
<?php
$post_ids = array();
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'dog-collars', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$post_ids[] = get_the_ID();
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_query();
print_r($post_ids);
?>
I can loop through the product_cat, pull id's into an array and then further down the page I use foreach and the WC product factory to manipulate data how I want it shown for users.
My problem is I need the loop to be Dynamic based on categories, and I can't understand how to do this.
I did think I can just grab the category name from the url
<?php $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
Grab it and the parse to just get the last , i.e category name, and then print into loop
But this seems like it would be a really poor way of doing it.
What I want is in the args
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'DYNAMICHERE', 'orderby' => 'rand' );
I want to be able to populate product_cat dynamically based on the category I am on
Any help or advise / pointing me in the right direction would be appreciated
Use get_query_var( 'product_cat' ).
I have a Parent page in Wordpress and am using the below code to display information about each of the child pages the parent has. Perfect.
What I need to do elsewhere on the page is display the count of child pages, for example 'This page has X child pages'.
Can anyone help me to do this please?
<?php
$args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) :
?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
//content goes here
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
You can use like
$pages = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'property'));
$count = count($pages);
I'm having a little trouble with a custom taxonomy template. I inherited a site that was developed by someone else and they use "Types" plugin to add some custom taxonomies.
Goal:
to have an archive template that shows only posts with a certain taxonomy term in it at example-domain.com/people/harrison-ford
Problem:
This code is bringing in posts that do not have the taxonomy selected.
Here's my full code:
<?php
$year = get_post_meta($post->ID, 'year', true);
$post_type = 'post';
$tax = 'people';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
$args = array(
'post_type' => $post_type,
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => 'date',
'order' => DESC
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h2 class="wwNews"><?php echo $tax_term->name; ?> News</h2>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<-- display stuff -->
<?php endwhile; // end of loop ?>
<?php endif; // if have_posts()
wp_reset_query();
}
?>
What are you expecting here? "$tax" is going to to be 'people' =>, which is going to overwrite 'harrison-ford' to the value of $tax_term->slug.
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
Furthermore, I don't know of any custom argument called people, I'm pretty sure that you want tax_query:
'tax_query' => array(
'taxonomy' => 'people',
'terms' => array('harrison-ford', $tax_term->slug)
)
Which will give you the results of all people matching harrison-ford and the value of $tax_term->slug within the taxonomy of people