WordPress show related posts based on taxonomy term - php

Below I have this code in single-courses.php which is currently displaying all the the courses. The goal is to show only related courses based on that courses' taxonomy term (category). Help please.
<?php
$related = get_posts( array(
'taxonomy' => 'course_category',
'post_type' => 'courses',
'numberposts' => -1
)
);
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<div class="row next-chapter-list" onclick="location.href='<?php the_permalink();?>';">
<div class="col-md-10 col-9 valign text-left">
<?php the_title(); ?>
</div>
<div class="col-md-2 col-3 valign text-right">
<i class="bi bi-play-fill"></i>
</div>
</div>
<?php }
wp_reset_postdata(); ?>

i found a solution literally after i posted this question, sorry- but this is what worked for me.
<?php
//get the post's venues
$custom_terms = wp_get_post_terms($post->ID, 'course_category');
if( $custom_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
// loop through venus and build a tax query
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'course_category',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// put all the WP_Query args together
$args = array( 'post_type' => 'courses',
'posts_per_page' => 5,
'tax_query' => $tax_query );
// finally run the query
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="row next-chapter-list" onclick="location.href='<?php the_permalink();?>';">
<div class="col-md-10 col-9 valign text-left">
<?php the_title(); ?>
</div>
<div class="col-md-2 col-3 valign text-right">
<i class="bi bi-play-fill"></i>
</div>
</div>
<?php
endwhile;
}
wp_reset_query();
}?>

Related

Why are the products not shown?

<?php
add_action('wp_ajax_nopriv_filter', 'filter_ajax');
add_action('wp_ajax_filter', 'filter_ajax');
function filter_ajax(){
$category = $_POST['category'];
$args = array(
'taxonomy' => 'product_cat',
'post_type'=>'product',
'posts_per_page' => -1,
);
if(isset($category)) {
$args['category__in'] = array($category);
}
The category id is delivered, but it is not clear to me why the products of this selected category are not displayed
$loop = new WP_Query($args);
if($loop->have_posts()):
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
<ul class="list-group list-group-flush list-group-products">
<li class="list-group-item select-blue-essence">
<div class="row in-list-text">
<div class="col-4 text-center font-poppins js-filter">
<span class="fw-bold"><?php the_title();?></span><br>
<span><?php the_content();?></span>
</div>
<div class="col-4 price-tag font-poppins fw-bold"><?php echo $product->get_price_html(); ?>
<?php woocommerce_template_loop_add_to_cart(); ?></div>
</div>
</li>
</ul>
</div>
<?php endwhile;
endif;
wp_reset_postdata();
die();
} ?>
$category = isset($_POST['category']) :?array(absint($_POST['category'])) : array();
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $category
)
)
);
$loop = new WP_Query($args);
Try this - Category IDs should be integer

ACF row to show thumbnails of recent posts

I'm trying to create a ACF flexible content row to display the most recent post thumbnails for a given category. However it keeps throwing a critical error and I'm not sure why.
<?php
$section_id = get_sub_field('section_id')
$categories = get_sub_field('categories');
$tags = get_sub_field('tags');
$postnum = get_sub_field('number_of_posts');
if (!is_array($categories)) {
$categories = array($categories);
}
$tags = get_field('my_tags_field');
if (!is_array($tags)) {
$tags = array($tags);
}
$args = array(
'post_type' => 'post',
'numberposts' => $postnum,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => $categories
),
array(
'taxonomy' => 'post_tag',
'terms' => $tags
)
)
);
$query = new WP_Query($args);
?>
<style>
</style>
<section class="post_row_with_thumbnails" id="<?php echo $section_id; ?>">
<div class="container-fluid">
<div class="row">
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<div class="col">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>" class="project_pics">
<h5 class="posttitle"><?php the_title(); ?></h5>
<h6 class="postdate"><?php the_date(); ?></h6>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
</div>
</section>
I have tried substituting WP_Query() with get_posts() but it gives me the same critical error.
In line 2 the ; at the end of the line, in this part:
$section_id = get_sub_field('section_id') ; THE DOT AND COMMA IS MISSING
This is the reason for the critical error.

Wordpress - get category name of custom post type

My code returns category name of default post type "post":
<div class="row masonary-wrap">
<?php
$args = array(
'post_type' => 'project',
'post_status' => 'publish',
'posts_per_page' => '6'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$categories = get_categories( $args );
foreach ( $categories as $cat ) : ?>
<div class="col-lg-4 col-md-6 col-12 port-item mb-30 <?php echo esc_attr( $cat->cat_name ); ?>">
<div class="project">
<div class="proj-img">
<div class="proj-overlay">
<h5><?php the_title(); ?></h5>
</div>
</div>
</div>
</div>
<?php
endforeach;
endwhile;
wp_reset_postdata();
endif; ?>
</div>
I need to display category name or better slug of custom post type "project", not of default posts!
you can do something like that. please check the below code
$terms = get_the_terms( $post->ID , 'project' );
foreach ( $terms as $term ) {
echo $term->name;
}
thanks,

Dispaly posts by different category in 1 page - Wordpress Development

I have created custom post type, and I am dispalying posts in archive page, but I wanna display posts by different categories in 1 single page. Like this:
But my posts are currently like this
How can i achieve that? I have searched a lot but didn't find any solution to do this. So, that's why I am putting question here.
Here is my code:
<section class="careerBlogs">
<div class="container">
<div class="row with-gutters">
<?php
$args = array (
'post_type' => array( 'career' ),
'post_status' => array( 'publish' ),
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
);
$templates = new WP_Query( $args );
if ( $templates->have_posts() ) {
while ( $templates->have_posts() ) {
$templates->the_post(); global $post;
$customVars = get_post_meta($post->ID, 'custom_vars', true);
if(!empty($customVars)){
$isRemotely = $customVars['remotely'];
}
// Categories
$categories = get_the_terms( $post->ID, 'career_category' );
foreach( $categories as $category ) { ?>
<div class="col-xs-12">
<div class="careerBlogs--title">Open Positions in <?= $category->name; ?></div>
</div>
<?php } ?>
<div class="col-xl-12">
<div class="card mb-4">
<a href="<?= get_the_permalink(); ?>" class="card-link">
<div class="card-info">
<div class="card-title"><?= get_the_title(); ?></div>
<div class="location">
<span>Lahore</span>/<span><?= $isRemotely ? 'Remote' : '' ?></span>
</div>
</div>
</a>
</div>
</div>
<?php } } else {
echo 'no posts to show';
}
wp_reset_postdata();
?>
</div>
</div>
</section>
It is in my archive-career.php page. Can you please help me to achieve that? I am stuck here
After struggling a lot, I have solved my problem with this:
<?php
// I get my Categories
$categories = get_terms('career_category' );
$currentCatName = '';
foreach( $categories as $category ) { ?>
<div class="row with-gutters">
<div class="col-xs-12">
<!-- Assiging category name -->
<div class="careerBlogs--title">Open Positions in <?= $category->name; ?></div>
</div>
<?php
$args = array (
'post_type' => array( 'career' ),
'post_status' => array( 'publish' ),
// here with 'tax_query' i solved my problem to show posts
// by categories (not to show double category names)
'tax_query' => array(
array(
'taxonomy' => 'career_category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$templates = new WP_Query( $args );
if ( $templates->have_posts() ) {
while ( $templates->have_posts() ) {
$templates->the_post(); global $post;
?>
<div class="col-xl-12">
<div class="card mb-4">
<a href="<?= get_the_permalink(); ?>" class="card-link">
<div class="card-info">
<div class="card-title"><?= get_the_title(); ?></div>
<div class="location">
<span>Lahore</span>/<span>Remote</span>
</div>
</div>
</a>
</div>
</div>
<?php } } else {
echo 'no posts to show';
} ?>
</div>
<?php }
wp_reset_postdata();
?>
See:

WP_Query orderby 'rand' not working

Trying to order some posts I'm displaying on a single custom post type page with random, but they aren't random at all. :/
<?php
// Grab the taxonomy term slug for the current post
$terms = get_the_terms( get_the_ID(), 'category-staff' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->slug;
}
$on_draught = join( ", ", $draught_links );
?>
<div class="container hidden-xs">
<div class="row">
<div class="col-sm-12">
<hr />
<h3 class="text-center">Other People At Our Great Resort</h3>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-lg-10 col-lg-offset-1">
<div class="row staff-list">
<?php
// WP_Query arguments
$args2 = array (
'post_type' => 'staff',
'tax_query' => array(
array(
'taxonomy' => 'category-staff',
'field' => 'slug',
'terms' => $on_draught,
),
),
'nopaging' => false,
'posts_per_page' => '4',
'order' => 'DESC',
'orderby' => 'rand',
);
// The Query
$query2 = new WP_Query( $args2 );
// The Loop
if ( $query2->have_posts() ) {
while ( $query2->have_posts() ) {
$query2->the_post(); ?>
<div class="staff staff-other col-sm-3 text-center">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo get_the_post_thumbnail( $_post->ID, 'large', array( 'class' => 'img-responsive img-circle img-staff' ) ); ?>
<h4><?php the_title(); ?></h4>
<?php if (get_field('staff_job')) { ?>
<p><?php the_field('staff_job'); ?></p>
<?php } ?>
</a>
</div>
<?php }
} else { ?>
<?php }
// Restore original Post Data
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
<?php endif; // terms if statement ?>
Turns out it was something to do with WPEngine. They disable rand() from the server and it needs to be enabled manually.
Another solution may be to add this code before running the new WP_Query($args) function.
remove_all_filters('posts_orderby');
https://developer.wordpress.org/reference/functions/remove_all_filters/

Categories