WP Query Custom Taxonomy but exclude current term - php

I am setting up a wp_query to list all of the terms in a custom taxonomy. However, I'd like to ammend the code below to exclude the current taxonomy term.
For example, when on the archive.php page for 'term1', I'd like that to be excluded, meaning I am left with 'term2', 'term3' etc.
<?
$args = array(
'taxonomy' => 'topic',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => '-1'
);
?>
<? $tax_menu_items = get_categories( $args );
foreach ( $tax_menu_items as $tax_menu_item ):?>
<li>
<a href="<? echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>">
<? echo $tax_menu_item->name; ?>
</a>
</li>
<? endforeach; ?>

Try this, it does depend on where you are using it
// Get the category ID
$catID = get_query_var('topic');
// Now run the query excluding the current taxonomy
$args = array(
'exclude' => $catID,
'taxonomy' => 'topic',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => '-1'
);
Then the rest of your code above. For more on get_categories() take a look at https://codex.wordpress.org/Function_Reference/get_categories the exclude parameter is definitely what you need - it is just making sure you exclude the correct category ID.

Related

Wordpress two custom_post_type one taxonomy

I create two custom post_type. The name of the custom post is-
1. Brand
2. Ethical
And I have created a taxonomy. The name of the taxonomy is - Pharma. The common taxonomy of the two custom posts is one (pharma).
Now I want, on one page -
1. Just to display all the names of Pharma Taxonomy.
2. I would like to display only brand custom posts under Pharma Taxonomy.
3. I would like to count only the post of brand custom post under pharma taxonomy.
All right. But when I just call the brand custom post_type with Pharma Taxonomy then the ethic custom post also becomes a call. I want a solution.
$args = array(
'post_type' => 'brand',
'taxonomy' => 'pharma',
'order' => 'ASC',
'posts_per_page' => -1,
);
$query = new WP_Term_Query($args);
foreach ($query->get_terms() as $term) : ?>
<div class="item_wrap">
<h2><?php echo $term->name; ?></h2>
<span class="count"><?php echo $term->count; ?> <?php _e('brands'); ?></span>
</div>
<?php endforeach;
WP_Term_Query does NOT take 'post_type' argument Here's a list of valid argumentsDocs. However, you could add a WP_QueryDocs inside your foreach statement. Like this:
$pharm_args = array(
'taxonomy' => 'pharma',
'orderby' => 'name',
'order' => 'ASC',
);
$pharm_query = new WP_Term_Query($pharm_args);
foreach ($pharm_query->get_terms() as $term) :
$count_args = array(
'post_type' => 'brand',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
)
);
$count_term_in_cpt = new WP_Query($count_args);
if ($count_term_in_cpt->found_posts) {
?>
<div class='item_wrap'>
<h2><?php echo $term->name; ?></h2>
<span class='count'><?php echo $count_term_in_cpt->found_posts; _e('brands'); ?></span>
</div>
<?php
}
wp_reset_postdata();
endforeach;
Which will output this:

Wordpress short code to display all child page by parents ID

I'm working on a short code for loop through parents ID and display all child page, but I'm not quite sure how to make the loop and make it more custom.
Here's my code:
add_shortcode( 'home-page-listing', 'get_list' );
function get_list( $atts ) {
ob_start();
$atts = shortcode_atts( array(
'ids' => ''
), $atts );
if($atts['ids']!='')
{
$id_array = explode(',',$atts['ids']);
$homePages = new WP_Query( array(
'post_type' => 'page',
'post__in'=>$id_array,
'order' => 'ASC',
'orderby' => 'post__in',
'posts_per_page' => -1
) );
if ($homePages->have_posts()){?>
<div class="">
<?php while ( $homePages->have_posts() ) : $homePages->the_post(); ?>
//here's html template code
<?php endwhile;
wp_reset_postdata(); ?>
</div>
}
}
}
Right now I can use [home-page-listing id=1,2,3,4] to display all select page ID, but I would like to make like this:
[home-page-listing parentID=4]
loop through all child page and display to the font, instead go check all the page id to display.
Thanks!
it's simple used post_parent Arguments of WP_Query. Please check below example
$homePages = new WP_Query( array(
'post_type' => 'page',
'post_parent'=>$parentID,
'order' => 'ASC',
'orderby' => 'parent',
'posts_per_page' => -1
) );
For more information of WP_Query click here

Show category by latest post date

I am working in a project where I am using below code to limit number of posts in each category. But I can't figure out how to sort these categories by latest posts. My code is working fine but when I add argument in wp query array, it does not sort or it breaks. I want to order the category with most recent post (date).
Can anyone guide me?
<?php $terms=g et_terms( array( 'taxonomy'=>'category', 'hide_empty' => false, ) ); foreach($terms as $cat){ $cata_name = $cat->name; $term_id = $cat->term_id; ?>
<?php //echo '<h3>'.$catname[0]->cat_name.'</h3>'; ?>
<?php $catqueryy=n ew WP_Query ( 'cat='.$term_id. '&posts_per_page=5');$count=$ catqueryy->found_posts; ?>
You can add ORDER BY argument in wordpress as:
<?php
$args = array(
'post_type' => 'post', //your_post_type
'orderby' => 'date', //meta_value
'meta_query' => array(array('key' => 'cat','value'=>$term_id)),
'order' => 'DESC',
'posts_per_page' => 5,
);
$catqueryy = new WP_Query($args);
?>
You can explore the wordpress document: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

WordPress: Category sort by Latest Post Date

I am working in a custom WP theme.I need to show each posts under individual categories, which is working fine.But, i want to sort the categories according to the published date of posts.
I found one answer, but its not working in my code and i cant figure how to use it in my code.So, i need anyone's expert help/input in below code.
Similar answer
**<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'orderby' => 'date',
'order' => 'ASC',
) );
foreach($terms as $cat){
$cata_name = $cat->name;
$term_id = $cat->term_id;
$catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=10');$count = $catqueryy->found_posts;while($catqueryy->have_posts()) : $catqueryy->the_post();?>
<div class="list-group">
<p class="post_excerpt">
<?php echo ' '.__(get_the_title(),'rockon').''; ?>
</div>
<?php
endwhile;
?>
You can try this to show all categories by date
<?php $args = array(
'show_option_all' => '',
'orderby' => 'ID',
'order' => 'DESC',
); ?>
<?php wp_list_categories( $args ); ?>

Having trouble with wordpress custom taxonomy archive

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

Categories