Custom shortcode for parent and child taxonomy not working - wordpress

I got a shortcode to print out the brand name connected to the current page:
add_shortcode( 'MARKE', 'marke_shortcode' );
function marke_shortcode() {
$terms = array(
'post_type' => 'fahrzeuge',
'taxonomy' => 'marken',
'hide_empty' => false,
'fields' => 'ids'
);
if( is_tax('marken') ) {
$terms['tax_query'] = array(
array(
'taxonomy' => 'marken',
'field' => 'term_id',
'terms' => (array) get_queried_object_id(),
)
);
}
$posts = get_posts( $terms );
if( ! empty( $posts ) )
return get_field( 'marken', $posts[0] );
return '0';
}
Unfortunately the code isn't working.
What I try to achieve is that the brand name, e.g. Tesla is printed out with the shortcode [MARKE] on both type of pages:
https://moinmobility.de/marken/tesla/
https://moinmobility.de/marken/tesla/model-3/
I tried a lot of code chunks and always if the brand name was printed correctly on the parent page, the child one was wrong.

Related

Display Woocommerce Related Products by current Category and tags

Please don't judge this question harshly.
I have tried showing in Related Products only products from the same category in which the user is viewing the product right now, and by product tags (if applicable). But nothing has changed for me.
It turns out that WooCommerce already displays recommended products from the same category and by the same tags by default.
Below you will find the answer to this question. But let this code remain here simply as a sample code.
function related_products_by_current_category( $related_posts, $product, $args ) {
global $post;
$cat = $product->get_category_ids();
$tags = wp_get_post_terms( $post->ID, "product_tag" );
foreach ( $tags as $tag ) {
$tags_array[] .= $tag->term_id;
}
$related_posts = new WP_Query(
array(
'orderby' => 'rand',
'posts_per_page' => 4,
'post__not_in' => array($post->ID),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cat
)
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
)
)
)
);
return $related_posts;
}
add_filter( 'woocommerce_related_products', 'related_products_by_current_category', 999, 3 );
WooCommerce already fetches related products based on the same terms( categories and tags )
If you want to get more control over the related products by categories and tags, you can use these two filters
apply_filters( 'woocommerce_get_related_product_cat_terms', wc_get_product_term_ids( $product_id, 'product_cat' ), $product_id );
apply_filters( 'woocommerce_get_related_product_tag_terms', wc_get_product_term_ids( $product_id, 'product_tag' ), $product_id );
These filters return the current product categories | tags IDs which will be used to get related products. you can use these filters to return specific terms IDs based on the given product ID .

Display Woocommerce Related Products By Custom Taxonomy

I want to display related products based on Custom taxonomy. Is there a way to force related products to be displayed based on Custom taxonomy?
add_filter( 'woocommerce_output_related_products_args', 'prfx_change_related_products_count' );
function prfx_change_related_products_count( $args ) {
// Current post as global
global $post;
$terms = get_the_terms( $post->ID, 'your_custom_taxonomy' );
// Empty array
$ids = array();
foreach ( $terms as $term ) {
array_push($ids, $term->term_id);
}
$args['tax_query'] = array(
array(
'taxonomy' => 'your_custom_taxonomy',
'terms' => $ids,
'field' => 'term_id',
'include_children' => true,
'operator' => 'IN'
)
);
return $args;
}

Wordpress Custom Tax for CPT Paging 404?

I have googled this issue for the last 4 days, and everything I have found has not fixed the issue.
My Custom Post Type's Custom Taxonomy template paging 404's unless I set the sites Blog posts per page to 1, which I cannot do, due to the fact that there will be a need to show more than 1 "blog post" per page...
How can I correct this issue?
LATEST FIX ATTEMPT
// fix the paging 404's on category/tag pages
add_action( 'pre_get_posts', function( $qry ) {
$_num_per_page = ( get_option( 'gyokb_option_name' )['gyokb_articles_pp'] ) ?? 5;
if( ( is_tax( 'kb_categories' ) || is_tax( 'kb_tags' ) ) && ! is_admin() ) {
$qry->set ( 'post_type', array( 'post', 'gyo_kb' ) );
$qry->set( 'posts_per_page', $_num_per_page );
}
} );
My CPT is here... (too much code to paste here...)
https://pastebin.com/SyKJwnCH
My Taxonomy is here...
https://pastebin.com/4BWWgGWq
And My Paging in the template:
<?php
echo paginate_links( array(
'format' => 'page/%#%',
'prev_text'=>' Previous ',
'next_text'=>' Next ',
'current' => max( 1, $page ),
'total' => $pg_ct,
'type' => 'plain',
'mid_size' => 4,
'paged' => $page, ) );
?>
with my WP_Query pulling it all together....
$args = array( 'post_type' => 'gyo_kb', 'posts_per_page' => $_num_per_page, 'paged' => $page,
'orderby' => 'date','order' => 'DESC',
'tax_query' => array(
array (
'taxonomy' => 'kb_categories',
'field' => 'slug',
'terms' => $cur_cat -> slug,
)
),
);
$the_query = new WP_Query( $args );
$page is populated with: $page = get_query_var( 'page' ); $page = ( ! empty( $page ) ) ? $page : 1;
All other variables are populating properly
I had the fix applying during the incorrect "boot" timing for wordpress. Initially I had placed in in the "wp" action.
I moved it to the "init" action, and now it works.

Wordpress: cutom query by custom metadata (meta field) of taxonomy term

I have a custom post type "member" with custom taxonomy "member_category". In this taxonomy I have a custom field (as metadata) called "display".
Now I want to query members from the DB and only recieve members who have the taxonomy with the metadata "display" with value of "1".
My attempt is:
$args = array(
'post_type' => 'member',
'tax_query' => array(
array(
'taxonomy' => 'member_category',
'value' => '1',
'field' => 'term_meta[display]' (????)
'operator' => 'LIKE'
)
));
$query = new WP_Query($args);
But it's not working.
Any idea? Maybe I should use a SQL query? If so, Which $wpdb query should I write?
Thanks
You should first fetch the terms having your desired meta value like this:
$term_args = array('taxonomy' => 'member_category');
$terms = get_terms( $term_args );
$term_ids = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) )
{
foreach( $terms as $term )
{
$val = get_term_meta( $term->term_id, 'display', true);
if( $val == '1' ) {
// push the ID into the array
$term_ids[] = $term->ID;
}
}
}
Then passing combined id's on the tax_query as below :
$args = array('post_type' => 'member');
$tax_query = array();
if(sizeof($term_ids))
{
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'member_category',
'field' => 'ID',
'terms' => $term_ids,
'operator' => 'IN',
)
);
}
if(sizeof($tax_query))
$args['tax_query'] = $tax_query;
You don't need to use SQL queries. If you are querying metadata, you need to use the meta query.
$args = array(
'post_type' => 'member',
'meta_query' => array(
array(
'key' => 'display',
'value' => '1',
)
));
On your args you have everything under tax query which is why it does not work.

Group Posts by meta_query

I'm trying to group my posts by a meta value, and can't seem to figure it out. My query below just returns "featured" posts. I would like the query to return all posts, but have the "featured" results before the rest.
Any ideas?
$args = array(
'posts_per_page' => '20',
'paged' => $current_page,
'meta_query' => array(
array(
'key' => 'tags',
'value' => 'featured',
'compare' => 'like'
)
),
'order_by' => 'date',
'order' => 'DESC'
);
Your answer is rewind_posts(). Run your query to get all the posts, run your loop and filter out all posts which is not featured, rewind the loop, rerun the loop and filter out featured posts.
Something like this will do
$args = [
// Your query arguments to get all postst
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
if ( get_post_meta( $post->ID, 'tags', true ) == 'featured' ) {
// Run your loop for featured posts
}
}
$q->rewind_posts(); // Rewind the loop to rerun it
while ( $q->have_posts() ) {
$q->the_post();
if ( get_post_meta( $post->ID, 'tags', true ) != 'featured' ) {
// Run your loop for non featured posts
}
}
wp_reset_postdata();
}

Resources