WordPress get category ID to Array - php

I have this code in category.php and I want to show posts from the category in which I am, "$cat" and the category referred to id 63. I wrote something but do not want to work :/
<?php
$cat->term_id;
$my_query_args = array(
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array(63, $cat),
'operator' => 'AND'
)
)
);
$my_query = new WP_Query($my_query_args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
Can someone tell me why do not incur the currently displayed category to $cat?
Thanks in advance for your help!

$category = get_category( get_query_var( 'cat' ) );
echo $cat_id = $category->cat_ID;
This will give you the current category id.
Now place the below query before the starting of wordpress loop in category.php
query_posts( 'cat='.$cat_id);
If thats normal category you dont want to make it complex with too many args. This will work dynamically by checking your current category id.

may be you mess with 'field' arguments in your tac_query.
'field' => 'term_id',
field- Select taxonomy term by. Possible values are 'term_id', 'name', 'slug' or 'term_taxonomy_id'. Default value is 'term_id'.

probably your $cat variable is an object. Try to change 'terms' => array( 63, $cat ), into 'terms' => array( 63, $cat->ID ), or 'terms' => array( 63, $cat->id ),

try this set your cat id or name at cat =..
<?php
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><h3><?php the_title(); ?></h3>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>

Related

One of the Wordpress Custom Post displaying out of order

I am facing a very strange problem, all my posts are displaying in proper order but one post is not following the order. Here is the picture of dashboard side:
You can see in the picture Chapter 3 is displaying in the correct order.
Now that's the picture on the front end:
In the dropdown you can see Chapter 3 Gases is displaying in last while it should display after chapter 2. I'm not getting what's the problem Here is my PHP code for dropdown:
<ul class="dropdown-menu" aria-labelledby="dLabel">
<?php
$terms = get_the_terms( $post->ID, 'subject' );
if ( !empty( $terms ) ){
// get the first term
$term = array_shift( $terms );
// echo $term->name;
}
$classes = get_the_terms( $post->ID, 'class' );
if ( !empty( $classes ) ){
// get the first term
$class = array_shift( $classes );
// echo $term->name;
}
$args = array(
'post_type' => 'lecture',
'posts_per_page'=> -1,
'orderby' => 'date',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'class',
'field' => 'slug',
'terms' => $class,
),
array(
'taxonomy' => 'subject',
'field' => 'slug',
'terms' => $term,
),
)
);
$data = new WP_QUERY($args);
$unique_chapter = array();
while($data->have_posts()):
$data->the_post();
$chapter_obj = get_field('chapter');
if( ! in_array( $chapter_obj, $unique_chapter ) ) :
// add city to array so it doesn't repeat
$unique_chapter[] = $chapter_obj;
?>
<li><a href="<?php echo the_permalink(); ?>">
<div class="text-left chapter_heading_topnav"><?php echo substr($chapter_obj->post_title,0,9) ;?></div>
<div class="text-left chapter_text_topnav"><?php echo $chapter_obj->post_content ;?></div>
</a></li>
<li class="divider"></li>
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>
</ul>
As you can see I'm setting order on date bases in ascending all other chapters are displaying properly only chapter 3 is out order. Please any clue why it's happening I'll be very thankful to you for your kind help.
I checked your site, it seems to be okay now. :)

Wordpress - How do I display posts grouped by custom taxonomy?

I am creating a FAQ page using a custom post type and custom taxonomy. I'm trying to create an unordered list for each taxonomy in order to group the FAQ's. In that unordered list, I want the first listed item to be the taxonomy name and then repeat the second listed item for all the questions in the taxonomy. This is the page I'm working on link.
It's currently duplicating the posts instead of displaying in the rightful taxonomies.
<?php
// get all the categories from the database
$cats = get_terms( array(
'taxonomy' => 'faq_categories',
));
// loop through the categories
foreach ($cats as $cat) {
// setup the category ID
$cat_id = $cat->term_id;
?>
<!-- Make a header for the category -->
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
// create a custom wordpress query
query_posts( array(
'post_type' => 'faqs',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories', //or tag or custom taxonomy
'field' => 'slug',
'terms' => 'for-women'
)
)
));
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category
wp_reset_postdata();
?>
</ul>
<?php } // done the foreach statement ?>
Your query is not changing as you iterate through the $cats array. Perhaps changing the the value of the 'terms' array to $cat->slug would give you better results.
Thank you so much. You have both provided great insight as to what I was missing. I've resolved it now and this is how I went about solving it taking your suggestions into consideration.
<?php
$cats = get_terms(
array(
'taxonomy' => 'faq_categories',
'orderby' => 'term_id',
'order' => 'ASC'
)
);
foreach ($cats as $cat) :
?>
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
$questions = new WP_Query(
array(
'category_name' => $cat->slug
)
);
$questions = new WP_Query( array(
'post_type' => 'faqs',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories',
'field' => 'slug',
'terms' => array($cat->slug),
)
)
));
?>
<?php if ($questions->have_posts()) : while ($questions->have_posts()) : $questions->the_post();?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
In your query_post, your tax_query's field should be term_id and your terms be assigned to your $cat_id variable, instead of a hardcoded term.

Get Post from another post type that shares the same taxonomy?

I've got Two custom Post Types set up in Wordpress, One being called Products and the other Suppliers. Slugs for these are 'product' and one being 'supplier'.
These two post types share a custom taxonomy called Suppliers which slug is 'supplier-tax'.This then has lots of children which are the different suppliers.
Basically, What I am trying to do is when you are on a single post page for a product, I need to pull in a post for the supplier as well. I thought that the best way to do this with how I have set it up is to select the appropiate supplier from the supplier taxonomy when on the product page. And this then queries and gets post from the 'Supplier' Post ttype with the same selected Taxonomy.
I wrote this query which brings in posts from the taxonomy, however It needs me to tell it which taxonomy and which slug etc to bring in plus it doesnt query the different post type which makes it useless, however it was a start:
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => 'supplier_1',
),
) );
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
I've tried to adapt and include parts from queries I've fond on previous sources but I can't seem to crack it. This is my attempt at it:
<?php
$terms = wp_get_post_terms( $post->ID, 'supplier-tax' );
if($terms){
$supplier_terms = array();
foreach ($terms as $term){
$supplier_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => $supplier_terms, //the taxonomy terms I'd like to dynamically query
'posts_per_page' => '-1'
),
),
'orderby' => 'title',
'order' => 'ASC'
) );
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?>
<?php the_title(); ?>"><?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
}
?>
Has anyone got any ideas on what I'm doing wrong or how I can make this work?
I managed to find a solution to my problem, whilst I don't think it is the cleanest markup, it works and does the job.
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
),
) );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$terms = get_the_terms( $post->ID, 'supplier-tax');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo $termID[0];
?>
<?php
$my_query2 = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
'field' => 'slug',
'terms' => '$termID',
),
) ); ?>
<?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>

Show all post in taxonomy category from certain id or slug

i have a custom post type called 'electronics' with taxonomy categories called 'computers, phones, notebooks...and more'
in the taxonomy.php template, ive managed to get the taxonomy name, slug and id with this code.
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
echo $term->name; // will show the name
echo $term->slug; // will show the slug
echo $term->term_id; // will show the id
?>
so if im in the computers taxonomy page, i get 'Computers computers 11'
with either pf the values generated each time how can i generate the posts according to which 'taxonomy page' im in. eg. posts tagged with 'computers' in the computers taxonomy page, posts tagged with phones in the phone taxonomy page.. and so on.
like something likes this but for taxonomies...
<?php
$catquery = new WP_Query( 'cat=3' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><?php the_title(); ?>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>
You need to use tax_query to get posts that have specific taxonomy term, here is how:
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'electronics',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$catquery = get_posts( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><?php the_title(); ?>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>

Wordpress - Increase amount of posts to be shown within specific category

I've been running in to a problem.
I'm editing a custom wordpress theme. I want to make some adjusments to the FAQ page (created with custom post type).
Right now every subject of question (= also category) is showing only 5 answers (these are posts).
I wanted to know, how I could increate this number, so instead of 5, show 10 or rather, show all answers per subject.
This is the code:
archive-faq.php
<?php $terms = get_terms( 'faq_category' ); ?>
<?php foreach( $terms as $term ):
$args = array (
'post_type' => 'faq',
'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args ); ?>
<h1><?php echo $term->name; ?></h1>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="toggle-holder">
<h5 class="toggle"><span><?php the_title(); ?></span></h5>
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
An important part is:
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
Where the_content() , is showing the posts that are in a certain category.
However it's nowhere to be found why , the page is only showing up to 5 posts (answers) and not any more ?
Things I've tried:
$query = new WP_Query( $args );
$query_posts('post_per_page=3'); ?>
Also:
putting 'showposts' => 8 under 'terms' => $term->term_id,
And I also tried:
$query = new WP_Query( $args ); ?>
<?PHP
query_posts( array(
'workcap' => $all_post_terms,
'showposts' => 8,
'caller_get_posts' => 1,
'post__not_in' => $do_not_duplicate ) ); ?>
->> In summary:
Why does the page only show up to 5 posts ?
And how do I change this property ?
PS. If you want to see the page: http://goo.gl/UnWRTz
The argument is posts_per_page and not post_per_page. Its a typo?
Try this:
$args = array (
'post_type' => 'faq',
'posts_per_page' => -1,
//for now try without this taxonomy, if it works try with it.
/*'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),*/
);
Btw, you can find more info here: https://codex.wordpress.org/Class_Reference/WP_Query

Categories