Custom post loop with custom taxonomy in wordpress - php

I am going wrong somewhere and it is driving me crazy. I am trying to pull posts from a custom post type with a custom taxonomy. I am pulling the taxonomy via an ID in the url and it just seems to show all posts of that post type no matter what I do. Here is my code:
<?php
$cruise = $_GET['reederei'];
$info = get_term_by( 'slug', $cruise, 'reederei' );
$info_id = $info->term_id;
$logo = get_field('logo', $term);
foreach ($taxes as $tax) {
$the_taxes[] = array (
'taxonomy' => 'reederei',
'field' => 'term_id',
'terms' => 'array( $info_id)',
);
}
$the_taxes['relation'] = 'OR';
$query = new WP_Query( array(
'post_type' => 'angebote',
'tax_query' => $the_taxes,
) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-sm-4">
<div class="offer_box_container offer_main">
<div class="offer_box_thumbnail"><img src="<?php the_field('featured_image'); ?>"/></div>
<h2 class="offer_inner_title"><?php the_title(); ?></h2>
<div class="offer_inner_sub highlight"><?php the_field('number_of_nights'); ?> Nächte: <?php the_field('travelling_to'); ?></div>
<div class="offer_description"><?php echo custom_field_excerpt(); ?></div>
<div class="col-sm-7 offer_button"><a class="direct_button" href="<?php the_permalink(); ?>" target="_blank">Zum Angebot >>></a></div>
<div class="col-sm-5"><div class="before_price">pro Person ab</div>
<div class="main_price"><?php the_field('price'); ?> -</div></div>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
<!-- show 404 error here -->
<?php endif; ?>

$the_taxes[] = array (
'taxonomy' => 'reederei',
'field' => 'term_id',
'terms' => 'array( $info_id)',
);
You turned your 'terms' => array() into a string. See if that helps.

Related

get specific custom type post in specific category - wordpress

I have a problem, i have a custom type post (authors) and taxonomy (manager and team) called position
I need to get all authors of the team on a page and need the manager to be at the first one of them
the authors ordered by name but the manager name begin with "T"
what can I do with this situation.
my code is
<?php
/**
* The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
* Template Name: taxonomies
*
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
get_header(); ?>
<?php
// echo the $slug name of the page
$page_names = get_the_title(); ?>
<section id="articles" class="articles">
<div class="<?php echo $classcase ?>">
<?php
$loop = new WP_Query( array( 'post_type'=>'authors','posts_per_page' => '-1','orderby' => 'title','order'=>'ASC' ) );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</section>
<div class='endauthors'></div>
For this task you need to use tax_query https://developer.wordpress.org/reference/classes/wp_tax_query/
First we take all managers sorted by title, then the entire team
Try this code
<?php
/**
* The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
* Template Name: taxonomies
*
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
get_header(); ?>
<?php
// echo the $slug name of the page
$page_names = get_the_title(); ?>
<section id="articles" class="articles">
<div class="<?php echo $classcase ?>">
<?php
$args = array(
'post_type' => 'authors',
'posts_per_page' => '-1',
'orderby' => 'title',
'order'=>'ASC'
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => array( 'manager' )
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
<?php
$args = array(
'post_type' => 'authors',
'posts_per_page' => '-1',
'orderby' => 'title',
'order'=>'ASC'
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => array( 'team' )
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</section>
<div class='endauthors'></div>

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.

post by taxonomy not showing at all in wordpress

Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>
</div>

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/

display content using get posts from terms under a custom taxonomy

I am having a custom taxonomy say "project-type" which is registered for the custom post "projects" and under that I have terms "catOne" and "catTwo". Now I want to display all the custom posts that are linked to catOne using the term_id of "catOne", which in my case is 9.
So I am successfully able to loop through all the posts but it is displaying only the ID but not all contents.
My approach:
$cat_id = 9;
$args = array(
'post_type' => 'projects',
'tax_query' => array(
array(
'taxonomy' => 'project-type',
'field' => 'term_id',
'terms' => array( $cat_id )
),
),
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<div id="post-<?php echo $post->ID; ?> <?php post_class(); ?>">
<h1 class="posttitle"><?php the_title(); ?></h1>
<div id="post-content">
<?php the_excerpt(); ?>
</div>
</div>
<?php } wp_reset_postdata();
Output I am getting
<div id="post-137 class=" ""="">
<h1 class="posttitle"></h1>
<div id="post-content">
</div>
</div>
<div id="post-135 class=" ""="">
<h1 class="posttitle"></h1>
<div id="post-content">
</div>
</div>
Can someone please help me with where I am going wrong?
Instead of using post_class(), the_permalink(), the_title(), and the_excerpt(), you should use the $post object to get the data, just like you did with $post->ID. The functions you've used should be called only if you're using a loop based on have_posts(). You aren't, so replace them with:
post_class() -> get_post_class( '', $post->ID )
the_permalink() -> get_the_permalink( $post->ID )
the_title() -> $post->title
the_excerpt() -> $post->post_excerpt

Categories