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.
Related
I have a page where I want to reference the titles from specific posts. This is my code with a loop right now -
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'cat' => '3',
);
$product_posts = get_posts( $args );?>
<p>
<?php foreach ( $product_posts as $post ) : setup_postdata( $post ); ?>
<?php echo get_the_title(); ?>
</p>
<?php endforeach; ?>
I don't want to loop through every post though, I want to be able to single out certain posts. For example, where I have the <p> get_the_title </p> I want to be able to display it like -
<p>Title of Post 5 vs Title of Post 6</p>
How can I do this?
You can try with below:
On the $catquery query the cat=3 is category ID so you can change with your specific category ID. And post_per_page=5 is total count of post so also you can change as per your required.
<?php $catquery = new WP_Query( 'cat=3&posts_per_page=5' ); ?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
Thanks and let me know if any query.
Try this with post ID
$postid= array(144, 246);
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'post__in' => $postid,
'posts_per_page'= 5
);
// The Query
$the_query = new WP_Query( $args );
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
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>
I have a this code in my WP theme and it works correctly. But when I want to show the last post from another website (for example: www.mag.tabgir.com) it doesn't show the post from my blog.
How can i change this code to show last post from this site www.mag.tabgir.com?
<section class="block-blog box">
<header>
<h2>recent post</h2>
</header>
<section class="content">
<?php
// The Query
query_posts( 'posts_per_page=3' );
// The Loop
while ( have_posts() ) : the_post();?>
<article class="clearfix">
<?php the_post_thumbnail('blog') ?>
<h1 class="title"><?php the_title(); ?></h1>
<span><?php the_time('d/M/Y') ?></span>
</article>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
see more
</section>
</section>
Below is the code it is working correct and tested locally.
$args = array(
'numberposts' => 1,// increase the number if you wish to display 2 latest
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
foreach ($recent_posts as $result)
{
echo 'Title : '.$result['post_title'].'<br/>';
echo 'content : '.$result['post_content'];
}
I've got some PHP code which I'm using in a WordPress website. Here's the code:
<h3>Case Studies</h3>
<?php
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<h3>Other Clients</h3>
<?php if( have_rows('clients') ):
while ( have_rows('clients') ) : the_row(); ?>
<a>
<h1><?php the_sub_field('client'); ?></h1><p><?php the_sub_field('sector'); ?></p>
<span></span>
</a>
<?php endwhile; endif; ?>
So - at the top, we've got "Case Studies" and this is just pulling in some details from the posts on the website (the client name and the sector).
Next, I've got "Other Clients" - this is set up as an Advanced Custom Field on the page that this code appears on. Pretty simple as well.
Now, here's the fun:
It works if I reverse the two sections ("Other Clients" first) but not this way - any ideas what's going wrong? I'm presuming it's something in the "Case Studies" section that's messing up the following one, but I'm at a loss. If I can provide any more info, let me know!
Much thanks in advance x
After finishing your custom query you must call wp_reset_postdata();
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
Because your custom query $the_query->the_post(); overrides global $post object, after finishing your query must always do this.
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'
),
),
) );
?>