Display terms used by another term on archive page - php

I've some custom taxonomies (doors qty) for vehicle (post type) and brands (another taxonomy).
When user click on Taxonomy: 2 or 4 doors, i need display on post type archive page all brands with 2 or 4 doors, for example: Honda, Toyota, Hyundai.
Unfortunately WordPress can't filter with this data to display the terms.
I try this:
<?php $args = array(
'taxonomy' => 'brand',
); ?>
<?php $terms = get_terms( $args ); ?>
<?php foreach ($terms as $term ) : ?>
<?php /* Ok, here display a name of brand from vehicles with 4 doors */ ?>
<?php echo $term->name; ?>
<?php endforeach; ?>
This code display all brands, but i need display only brands with 4 doors.
Watching documentation here: https://developer.wordpress.org/reference/functions/get_terms/ i see this:
'meta_query' => '',
'meta_key' => array(),
'meta_value'=> '',
Is there the correct way to filter brands with 4 doors?
Please consider that i use this only on archive page (archive.php) and i can use queried object to pass any data ;)

Okay
I resolve this, thinking terms wasn't correct way. The correct way is make a Custom Post Type called Brands and make it relational with another Custom Post Type or Term (in my case: Door Qty).
If you are interesting in Custom Post Types, please see this documentation: https://codex.wordpress.org/Post_Types
Excuse my english.

Related

Order custom post types in WordPress by the meta_value of another plugin

I've got a List of custom post types which I am displaying on my site. Additionally to those post types, I added a plugin to my WordPress which allows me to add a thumbs up rating system to each post type, so that it looks like this:
The code for looks like this:
<?php
/* The custom post types query */
$query = new WP_Query( array(
"post_type" => "motto",
"order" => "ASC",
));
while($query -> have_posts()) : $query -> the_post();
?>
/* Container with the ratings from the plugin + each post type */
<div id="motto-container">
<?=function_exists('thumbs_rating_getlink') ? thumbs_rating_getlink() : ''?>
<h3 class="abimottos">
<?php echo get_post_meta($post->ID, 'motto_titel', true); ?>
</h3>
</div>
I've got a list of these custom posts + their ratings, and of course each post has an individual rating and I want to order my custom post types after the value of those ratings. How could I archive that?
I know that the meta_key for the ratings is _thumbs_rating_up (since I've already modified the value with the ARI Adminer plugin), can I somehow use this meta_key to order the custom post types after the meta_value of the ratings?
I'm pretty new to PHP and databases.
You are already using WP_Query to get the posts, so you can specify the meta_key to sort by in the $args array, e.g.
$query = new WP_Query( array(
'post_type' => 'motto',
'meta_key' => 'thumbs_rating_up',
'orderby' => 'thumbs_rating_up',
'order' => 'DESC'
));
Note that you need to include the key name in both meta_key and orderby. I also assume you want to sort in descending order to show the highest ratings first.
Ref: Wordpress Codex for WP_Query
Also, a note on the meta_key:
meta_keys prefixed with an underscore are private and hidden from the custom fields, so normally you would use the version without an underscore. This might not be the case here because I assume the rating can't be changed in the admin, but just make sure that the meta_key you need to use is actually _thumbs_rating_up and not thumbs_rating_up.

Display all taxonomy based on post type

I just want to display a taxonomy term based on post type so that I can display and filter the items based on their taxonomy.
My code below displays EVERY taxonomy. I want it to be only a specific post type Publication
<?php
$taxonomy = 'articletype';
$tax_terms = get_terms( $taxonomy, array(
'post_type' => 'publication'
) );
?>
<?php
foreach ($tax_terms as $tax_term) {
?>
<button class="button" data-filter=".<?php echo $tax_term->slug;?>" >
<?php echo $tax_term->name;?>
</button>
<?php } ?>
Try using Codex Function Reference/get object taxonomies.
Usage:
<?php get_object_taxonomies( $object, $output ); ?>
Parameters:
$object: (array|string|object) (required):
Name of the post type, or a post object (row from posts).
$output: (string) (optional): The type of output to return, either taxonomy 'names' or 'objects'.
Return Values:
(array): All taxonomy names or objects for the given post type/post object.
also you can visit the original codex page to get examples and more info.
I'm not sure if it's still doable in your case (depends on if your site is still on dev or has too much content already), but I think it would be better to create custom taxonomy for your custom post type, it would make it easier for you to orgaanize your content.
I tried to look for a function in the wp codex that sorts taxonomy by post type but I couldn't find it.

shortcode to show products by category woocommerce

I found on commerce website two following code
[Products ids="1, 2, 3, 4, 5"]
[Products skus="foo, bar, baz" orderby="date" order="desc"]
One is used to show product by ids and second by foot. I want to show the product with category, but I could not find any shortcut for this.
E.g [products product_cat="shoes"]
Can anyone help me?
Did you try like this:
PRODUCT CATEGORIES BY SLUG
This will output the shortcode: [[[product_category category=”” per_page=”12″ columns=”4″ orderby=”date” order=”desc”]]]. Similar to product categories but this will output all products within the specified category and can be arranged with the per_page=”” and columns=”” parameters. The category slug can be found by navigating to the product menu in your WP admin and clicking on categories where you’ll see a list of all available categories and corresponding slugs.
See the below image which has option also
Refer this link too for more shortcodes : https://gplclub.org/woocommerce-shortcodes-full-list/
You can create your own shortcode by sing following code. the code will return all the products in given category
<?php
function view_woocommerce_products($args){
$cat = $args['category'];
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_ cat' => $cat );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
// Here you can access the product detail by using the $product variable
<?php endwhile; ?>
<?php wp_reset_query();
}
add_shortcode('view-woocommerce-products', 'view_woocommerce_products');
Here the use of shortcode
[view-woocommerce-products category='[your category]']

WordPress get_title from category

I was wondering how to get the title/img/content of some random article, but from specific category.
For an example:
I have 3 categories A,B and C and I have a image slider on my blog.
I want to show on the slider ONLY those articles who are in category A, not B and C.
How can I make that happen ? :)
The below example should get you started. It basically calls the get_posts() function with some criteria.
Return 5 Posts
Random Order
From Specificed Category
Then we run a foreach on the returned posts to do what we want. You don't have to run a foreach, in the below example $rand_posts will hold an array post objects with which you can do what you want.
You can take a look at the codex and change the arguments, criteria, to whatever you'd like.
Wordpress Codex - Get Posts
<?php
$cat_id = // Your category ID.
$args = array('numberposts' => 5, 'orderby' => 'rand', category => $cat_id);
$rand_posts = get_posts($args);
foreach($rand_posts as $post) : ?>
<li><?php the_title(); ?></li>
// Access all other post information here just like in a normal look. (Ex. the_content(), the_excerpt(), etc, etc
<?php endforeach; ?>

order results by category, wordpress

I have a category page called 'features' this is all fine, but the posts in the features category also belong to a certain genre of film, and it's this what I want to order the posts by on the actual features category template.
eg
features cat template brings in all features posts.
then what I want to do is display in alpha order by whatever genre it also belongs to
features
action
post
post
post
comedy
post
post
sci-fi
post
post
etc.
this is what I have at the moment ( the cat numbers relate to the genres = action=10 etc)
query_posts('cat=10,11,12,13,14,15,16,17,18&orderby=title&order=DESC' );
while (have_posts()) : the_post();
How can I list all the posts (group them) by genre ? when I use title here i guess it's using the posts title.
Playing around if I stick this in the post loop
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(4, $childcat)) {
echo $childcat->cat_name;
}
}
this returns the actual genre cat for each post while in the loop, but I'm not sure how to stick it together so I can state the ordering of the posts by groups of genre, I was hoping I could do this in the query_posts?
Any help in the right direction would be greatly appreciated.
I would consider first doing a query for your sub categories, and then loop through your sub categories querying the posts for each. Like so:
$categories = get_categories( array ( 'child_of' => 10, 'orderby' => 'name' ) );
foreach( $categories as $category ){
// query_posts for category by $category->term_id
// Display posts for this category
}
Does that work for what you are wanting to do?

Categories