I am trying to display 6 featured products with their thumbnails, price and title. But I am not able to see anything, however if I use $loop->found_posts I can see there are 6 records fetching back from database.
I have also added these lines in wp-config.php to see the errors but I am not able to see any error on the page
wp-config.php
define('WP_DEBUG', true);
if (WP_DEBUG) {
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);
#ini_set('display_errors', 0);
}
here is my code for displaying featured posts
<?php
$args = array (
'post_type'=>'product',
'meta_key'=>'_featured',
'posts_per_page'=>6
);
$loop= new WP_Query($args);
//echo $loop->found_posts;
while($loop->have_posts()): the_post();
echo '
<div class="col-xs-4">
<div class="custom-product">
<img src="'.woocommerce_get_product_thumbnail(300,335).'">
<div class="price-title">
<h2>'.the_title().'</h2>
<h3>'.$product->get_price_html().'</h3>
</div>
</div>
</div>
';
endwhile;
?>
Few suggestions here:
You should use:
while( $loop->have_posts() ): $loop->the_post();
instead of:
while( $loop->have_posts() ): the_post();
because otherwise you are setting up posts related to the global $wp_query object.
Note that the_title() doesn't return the value, it is echo-ing it.
Make sure $product is defined in your loop.
Use wp_reset_postdata() after your secondary query, to restore the global $post variable.
You can use the posts property of WP_Query:
var_dump( $loop->posts );
to peek into the array of queried posts.
To check the generated SQL query, we can use the request property of WP_Query:
echo $loop->request
This can come handy when we need to debug our query.
This should work as expected. woocommerce_get_product_thumbnail echoes the image tag HTML, what you need to pass as parameter is an existing thumbnail name, and width and height of the placeholder.
<?php
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'posts_per_page' => 6
);
$loop = new WP_Query( $args );
global $product;
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-xs-4">
<div class="custom-product">
<?php echo woocommerce_get_product_thumbnail( 'shop_catalog', 300, 335 ); ?>
<div class="price-title">
<h2><?php the_title(); ?></h2>
<h3><?php echo $product->get_price_html(); ?></h3>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
You can set custom image dimensions as stated here -> WooCommerce Codex
shop_catalog My first hit on google returned the following link.
Based on their instructions i updated your code.
The following code should work:
<?php
$args = array (
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => 6
);
global $post;
$loop= new WP_Query($args);
//echo $loop->found_posts;
while($loop->have_posts()): $loop->the_post();
$post = $loop->post;
setup_postdata( $post );
$product = get_product( $loop->post->ID );
echo '
<div class="col-xs-4">
<div class="custom-product">
<img src="' . woocommerce_get_product_thumbnail('shop_catalog ', 300,335) . '">
<div class="price-title">
<h2>' . get_the_title(). '</h2>
<h3>' . $product->get_price_html() . '</h3>
</div>
</div>
</div>
';
endwhile;
wp_reset_postdata();
?>
I got the same problem. Try this ! Works for me
<?php
$featured_query = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN'
),
),
) );
?>
Related
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.
I am trying to place a link to the Artist of a video in a custom post type in WordPress. Everything is working but the term_id does not iterate.
<?php
$args = array(
'post_type' => 'video',
'posts_per_page' => 4,
'taxonomy' => $term->name,
'number' => $term->term_id,
);
$q = new WP_Query( $args);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
//Your template tags and markup like:
?>
<div class="card">
<div class="bg-img"><img alt="Norway" style="width:100%" src="<?php the_post_thumbnail( 'thumbnail' );?></div>
<div class="content">
<h4 style="color: green;"><?php the_title( ); ?></h4>
<h5 class="artist-name"><?php
$terms = get_terms( $args );
foreach( $terms as $term ){
echo '<div style="color: black;">' . esc_html( $term->name ) . " ". $term->term_id . '</div>';
} ?>
As per your code, it seems that you are using the posts arguments for "get_terms" which is not correct and that's why you are not getting the correct term's data.
You can check the query parameter of terms over here to get the idea.
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.
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
My homepage is displaing my listings( my custom post type) by the order i enter them. I would like my listings that have the custom taxonomy "tag" (Special offer) to be displayed on my first page from my homepage and after that the rest of the listings exactly how they where before. I am new in to wordpress and hope i asked my question right
This is my homepage code
<div id="content">
<?php include (TEMPLATEPATH . '/lib/slider.php'); ?>
<?php
$args = array(
'post_type' => 'listings',
'paged' => $paged,
'showposts' => 8 ,
'oferta' =>"oferta"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post propbox <?php if (++$counter % 2 == 0) { echo "lastbox"; }?> clearfix" id="post-<?php the_ID(); ?>">
<div class="archimg">
<?php if( has_term( 'featured', 'type', $post->ID ) ) { ?>
<span class="featspan">Featured</span>
<?php } else if ( has_term( 'sold', 'type', $post->ID ) ){ ?>
<span class="soldspan">Sold</span>
<?php } else if ( has_term( 'reduced', 'type', $post->ID ) ){ ?>
<span class="redspan">Reduced</span>
<?php } ?>
<?php
if ( has_post_thumbnail() ) { ?>
<img class="propimg" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&h=180&w=310&zc=1" alt=""/>
<?php } else { ?>
<img class="propimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.jpg" alt="" />
<?php } ?>
</div>
<div class="cover">
<div class="title">
<h2><?php the_title(); ?></h2>
</div>
<div class="propmeta">
<div class="proplist"><span>Price</span> <span class="propval"> <?php $price=get_post_meta($post->ID, 'wtf_price', true); echo $price; ?></span></div>
<div class="proplist"><span>Location</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'location', '', ' ', '' ); ?></span></div>
<div class="proplist"><span>Property type</span> <span class="propval"><?php echo get_the_term_list( $post->ID, 'property', '', ' ', '' ); ?></span></div>
<div class="proplist"><span>Area</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'area', '', ' ', '' ); ?></span></div>
</div>
<div class="entry">
<?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
<a class="morer" href="<?php the_permalink() ?>">Check this</a>
<div class="clear"></div>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>
This is my main content content sorry how can i rearenge this so it fits my needs
you can use tax_query like by here
$args = get_posts( array(
'post_type' => 'my_post_type',
'tax_query' => array(
array(
'taxonomy' => 'my_taxonomy',
'field' => 'slug',
'terms' => 'webdesign'
)
)
) );
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
You have to pass the arguments array to you $wp_query->query($args); in which you define the tag name or category name , taxonomy of tag or taxonomy of category
$args = array(
'post_type' => 'your custom post type name',
'paged' => $paged,
'showposts' => 8 ,
'your custom goes here taxonomy' =>"tag name or category name"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
Generally taxonomy name is what you defined in register_taxonomy('here_goes_taxonomy',array('post_type'), array(...))
All the answers above are great, but you don't necessarily need to pass the tax_query array. Once you have created your custom taxonomy the right way, you can do the following.
<?php
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$loop = new WP_Query([
'post_type' => 'your_custom_post_type',
'post_status' => 'publish', // fetch only published posts
'posts_per_page' => get_option('posts_per_page') ?: 10, // number of posts to fetch
'paged' => $paged
'your_custom_taxonomy' => 'your_custom_taxonomy_slug_or_name'
]);
// WordPress pagination doesn't work on custom query
// Let's make it work
$temp_query = $wp_query; // store $wp_query in a temporary variable
$wp_query = null; // set it to null
$wp_query = $loop; // store your custom query to $wp_query
if ( $loop->have_posts() ) { // Let's check if we have some posts
while( $loop->have_posts() ){
$loop->the_post();
// add your markup here
}
}
wp_reset_postdata();
$wp_query = null;
$wp_query = $temp_query; // store back the original $wp_query
<?php $cat_terms = get_terms(
array('mobile_category'),
array(
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'number' => 2 ) );
if( $cat_terms ) : ?>
<?php foreach( $cat_terms as $term ) :
$args = array(
'post_type'=> 'mobile',
'posts_per_page'=> 2,
'post_status'=> 'publish',
'tax_query'=> array(
array(
'taxonomy' => 'mobile_category',
'field' => 'slug',
'terms' => $term->slug,), ),
'ignore_sticky_posts' => true
);
$_posts = new WP_Query( $args );
if( $_posts->have_posts() ) :
while( $_posts->have_posts() ) : $_posts->the_post(); ?>
<span class="color2"><?php echo $term->name ; ?></span>
<?php endwhile;
endif;
wp_reset_postdata();
endforeach; ?>
<?php endif; ?>