display content using get posts from terms under a custom taxonomy - php

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

Related

Exclude specific Woocommerce product category term(s) from a WP Query

I'm trying to load latest product in Woocommerce (on home page). I want to exclude specific product category from display on the loop, but exclude product category id is not working for me.
What I have done so far:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'post_status' => 'publish',
'taxonomy' => 'product_cat',
'exclude' => 29,//exclude mention category id from loop
'parent' => 0
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
<div class="popular-inner">
<a href="<?php echo get_permalink(); ?>">
<div class="popular-image d-flex ">
<div class="align-self-center mx-auto ">
<?php the_post_thumbnail(); ?>
</div>
</div>
<div class="popular-content-wp">
<div class="popular-title">
<h6><?php echo get_the_title(); ?></h6>
</div>
<div class="popular-price">
<p><?php echo wc_price($product->get_price()); ?></p>
</div>
<div class="popular-add-to-cart">
<ul>
<li>
Add to Cart
</li>
</ul>
</div>
</div>
</a>
</div>
<?php endwhile; wp_reset_query();?>
Since wordpress version 3.1 using a taxonomy slug in a WP_Query is deprecated in flavor of tax_query. Also there are some other mistakes. See WP_Query taxonomy parameters.
Your revisited code:
<?php
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 12,
'post_status' => 'publish',
'parent' => 0,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 29 ), // Term ids to be excluded
'operator' => 'NOT IN' // Excluding terms
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
// Get the instance of the WC_Product from the post ID
$product = wc_get_product( get_the_id() );
?>
<div class="popular-inner">
<a href="<?php echo get_permalink(); ?>">
<div class="popular-image d-flex ">
<div class="align-self-center mx-auto ">
<?php the_post_thumbnail(); ?>
</div>
</div>
<div class="popular-content-wp">
<div class="popular-title">
<h6><?php echo get_the_title(); ?></h6>
</div>
<div class="popular-price">
<p><?php echo wc_price( $product->get_price() ); ?></p>
</div>
<div class="popular-add-to-cart">
<ul>
<li>
Add to Cart
</li>
</ul>
</div>
</div>
</a>
</div>
<?php
endwhile;
wp_reset_postdata();
else: ?>
<p><?php _e( 'No posts found.' ); ?></p>
<?php endif;
?>
It should work now.

Custom post loop with custom taxonomy in wordpress

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.

How do I display all posts based on category?

For example i will have 4 categories. For each category i will show 5 recent post. I will have category such as breakfast, dessert, lunch and savory food. There will be a "see all" link for each category, so the user can see all the breakfast category post. On the main page i will list 5 recent post and when the users click the "see all" link it will link them to the whole breakfast category. I want all breakfast in that category if the they click "see all". The same for other categories.
Currently my code looks like this, but i'm stuck with the "see all" link. I don't know how to link it to main category.
<?php
get_header();
?>
<!-- recipe -->
<section class="recipe-wrap">
<?php
/*
* Loop through Categories and Display Posts within
*/
$post_type = 'recipe';
$category_link = get_category_link($cat->cat_ID);
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) : ?>
<div class="recipe-category owl-carousel-slide">
<div class="row">
<h2><?php echo $term->name; ?>see all</h2>
<div class="recipe-category-carousel owl-carousel owl-theme">
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => 10, //show all posts
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<div class="item recipe-box">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo(types_render_field('artwork', array('raw' => true) )); ?>">
<p><?php the_title(); ?></p>
</a>
</div>
<?php endwhile; endif; ?>
</div>
</section>
<?php endforeach;
endforeach; ?>
</div>
</div>
</div>
</section>
<!-- /recipe -->
<?php
get_footer();
?>
Try below code
foreach ( $terms as $term ) {
// Get term link by using get_term_link()
$term_link = get_term_link( $term );
echo '' . $term->name . '';
}
<div id="mini_stream">
<ul>
<? $args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'category_name'=>'Product',
);
$loop = new wp_Query($args);
while($loop->have_posts()) : $loop->the_post();
echo '<a href="'.get_permalink().'">';
echo get_the_post_thumbnail($post->ID, 'category-thumb');
the_title( '<h6>', '</h6>' );
echo '</a>';
endwhile;
wp_reset_query(); ?>
</ul>
</div>
=> Using this method may you get all post

Show post if true/false is yes. Advanced custom fields

I have managed to get my loop to show only the posts that display true on an advanced custom field.
But I now only want to show one post. I cant seem to get it to only loop one of the posts that features the true/false field as yes.
'posts_per_page' => '1'
Doesn't work as it only shows the latest post.. which if its not ticked it just shows blank.
<?php
$args = array(
'post_type' => 'event'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( 'yes' == get_field('sponsored_event') ): ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
You should use Meta Query here. Change your args array to:
// args
$args = array(
'numberposts' => 1,
'post_type' => 'event',
'posts_per_page' => '1'
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
The ACF Radio button Yes/No field is to be manipulated the other way. You have to get the Output and then compare with the value that you need.
Syntax:
$variable = get_field('field_name', $post->ID);
Where the $post->ID will be appering from the Loop that you use.
if (get_field('sponsored_event') == 'yes') {
// code to run if the above is true
}
else
{
// code for else part
}
Make sure that the value saved into the DB for the radio button is like you give in the if statement
This is an Optional for you to use the meta_value in the Query. if you dod't use you can fetch the acf value with the help of the post ID that you get from the loop of Wp_Query
Change the Wp_Query like this if you need only one post that to the latest one that has been stored.
$args = array( 'post_type' => 'event', 'posts_per_page' => 1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');
Else if you want all the posts that has been stored you can use like this.
$args = array( 'post_type' => 'event', 'posts_per_page' => -1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');
Note:
post_per_page=10 -> Will bring the 10 posts from your post type
post_per_page=-1 -> will bring infinite posts that your post type has
Entire Loop:
<?php
$args = array(
'posts_per_page' => 1,
'post_type' => 'event',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Your If statement in the query seems to be Incorrect and i have added the Query Executed output over to that if statement.

WordPress get by and exclude by meta data messes pagination

So this is my first post and wow, I didn't know this site existed. I've had a look around at questions and I hope that mine isnt a dumb nooby one. Although I am a noob :S
Ok, so I created a function in WordPress that will add a meta box to the new posts page so that I can specify whether or not this post should be featured (I read this is better than creating a featured category for SEO purposes?).
Anyway.. The code I have works in showing the most recent. Here is the code for that:
<?php
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$custom = get_post_meta($my_query->post->ID, '_featuredpost_meta_value_key', true);
if ( $custom ){
?>
<article class="container" itemprop="blogPosts" itemscope itemtype="http://schema.org/BlogPosting">
<div class="row">
<h2 itemprop="about">
<?php the_title(); ?>
</h2>
</div>
<div class="row">
<div class="<?php if ( has_post_thumbnail() ) { ?>two-thirds column<?php } else {?> twelve columns <?php } ?>">
<p class="post-excerpt"><?php modified_excerpt(); ?></p>
</div>
<?php if ( has_post_thumbnail() ) { ?>
<div class="one-third column">
<?php the_post_thumbnail('full', array('class'=>'hide-mobile')); ?>
</div>
<?php } ?>
</div>
<div class="row">
Continue Reading
</div>
<hr />
<div class="post-info">
<ul>
<li class="date"><?php the_date();?></li>
<li class="author"><?php echo get_the_author_meta('display_name'); ?></li>
<li class="category"><?php the_category(', '); ?></li>
<li class="tags"><?php the_tags('',', ',''); ?></li>
</ul>
</div>
</article>
<?php
}
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Now, When I use the same code below, but then use:
if ( ! $custom ){
to show the posts that are not set to be featured that also works. The problem is that the pagination no longer works. When I go to the second page it just duplicates what is on the home page.
This leads me to believe that I have created a mashed together crappy bit of code. Can someone please help me build a loop, that will exclude any posts where the meta data _featuredpost_meta_value_key is set to Yes.
Thanks in advance
You'll want to use a WP Meta Query in your original $args array.
https://codex.wordpress.org/Class_Reference/WP_Meta_Query
From the docs, here's an example:
$meta_query_args = array(
'relation' => 'OR', // Optional, defaults to "AND"
array(
'key' => '_my_custom_key',
'value' => 'Value I am looking for',
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
But you can also use the sugar provided by the WP_Query class and pass it in as the meta_query value to your original args:
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'meta_query' => array(
'key' => '_featuredpost_meta_value_key',
'value' => 'Yes',
'compare' => '='
)
);

Categories