I am trying to display same custom post query multiple time in same page. The between the post is it will call different custom taxonomy based on one custom post type. When I am trying to call same custom post second time its not working. But showposts =2 is working but I need to display one post for a single post query. Here is the codes:
<div class="col-sm-6">
<?php
$querevent = new WP_Query( array(
'post_type' => 'tatstory', // name of post type.
'showposts' => 1,
) );
if ( $querevent->have_posts() ):
// Yep, we have posts, so let's loop through them.
while ( $querevent->have_posts() ) : $querevent->the_post(); ?>
<?php if ( has_term('event','eventname' ) && has_term('featured-in-story-page','fetstory' )) { ?>
<div class="listing_inner">
<div class="listing_img">
<?php the_post_thumbnail( 'shop-story', array( 'class' => 'img-fluid' ) ); ?>
</div>
<div class="listing_texts">
<p class="event yellow">Event</p>
<h2><?php the_title(); ?></h2>
</div>
</div>
<?php
} ?>
<?php endwhile;
else :
// No, we don't have any posts, so maybe we display a nice message
echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
endif;
?>
</div>
<div class="col-sm-6">
<?php
$quernews = new WP_Query( array(
'post_type' => 'tatstory', // name of post type.
'showposts' => 1,
) );
if ( $quernews->have_posts() ):
// Yep, we have posts, so let's loop through them.
while ( $quernews->have_posts() ) : $quernews->the_post(); ?>
<?php if ( has_term('news','eventname' ) && has_term('featured-in-story-page','fetstory' )) { ?>
<div class="listing_inner">
<div class="listing_img">
<?php the_post_thumbnail( 'shop-newscat', array( 'class' => 'img-fluid' ) ); ?>
</div>
<div class="listing_texts_right">
<p class="event blue">News</p>
<h2><?php the_title(); ?></h2>
</div>
</div>
<?php } ?>
<?php endwhile;
wp_reset_postdata();
else :
// No, we don't have any posts, so maybe we display a nice message
echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
endif;
?>
First i would change your WP_Query, example:
$query = new WP_Query( array(
'post_type' => 'tatstory',
'posts_per_page' => '1',
'tax_query' => array(
array (
'taxonomy' => 'eventname',
'field' => 'slug',
'terms' => 'news',
),
array (
'taxonomy' => 'fetstory',
'field' => 'slug',
'terms' => 'featured-in-story-page',
)
),
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// display content
}
} else {
// display when no posts found
}
wp_reset_postdata(); // Restore original Post Data
The query will only collect posts that have the correct terms set.
Restoring original Post Data on the end (wp_reset_postdata()) may help you with calling a new WP_Query again. But I don't know of any reason why you cannot call it twice in a single page template.
Related
Try to achieve this:
I made a ACF (custom field) in my WooCommerce Product and now I try to get this field shown in my template with this code:
<ul class="products">
<?php
$args = array(
'posts_per_page' => 20,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'grouped',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$linked_with_items = the_field('linked_with_items');
the_title('<strong>', '</strong>'); echo '<br />';
echo $linked_with_items;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
But whatever I try also with get_field() the field does not show in my template.
Can someone help?
https://www.advancedcustomfields.com/
This is the final code fyi
<?php if( have_rows('catalogue') ): ?>
<?php
while( have_rows('catalogue') ): the_row(); // catalogue is the field
the_sub_field('linked_with_items'); ?>
<?php endwhile; ?>
<?php endif; ?>
You could try with this:
$linked_with_items = get_field('linked_with_items', get_the_ID());
If that doesn't work, just as a test, you could try to simply loop over posts with a foreach
foreach ( $loop->posts as $post ) {
$linked_with_items = get_field('linked_with_items', $post->ID);
}
If none of those work, please make sure that your product actually have that custom field, double check the ACF field settings (rule section), the field slug, and double check your product edit page to see if the fields shows there.
I'm working with taxonomy page order by terms whit different WP_Query. AT the last of every loop something add a "post" that have page title and info from the before post. Very strange thing.
I don't find nothing about this (neither I know how to search this). Thanks por your time. My doce is this:
<?php
$the_query = new WP_Query( array(
'post_type' => 'intervencion',
'tax_query' => array(
array (
'taxonomy' => 'especialidad',
'field' => 'slug',
'terms' => 'bariatrica_ok',
)
),
) );
while ( $the_query->have_posts() ) : $the_query->the_post();?>
<?php $meta = get_post_meta(get_the_ID()); ?>
<div class="col-lg-3 col-md-4 col-sm-6">
<div class="ficha">
<?php if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('thumb_intervenciones', array('class' => 'img-responsive')); ?>
<?php } ?>
<h2><?php echo get_the_title(); ?></h2>
<p class="condiciones">Todo</br>incluido por:</p>
<p class="precio"><?php echo $meta['intervenciones_precio_intervencion'][0]; ?> €</p>
<a class="ficha-btn" href="<?php the_permalink()?>">Solicitar cita</a>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
The result at the bottom of the list is:
enter image description here
I'm creating a dynamic section in my homepage with only posts format video. Now i need to get the URL from this videos...
I already got the Title, link and the date:
<div class="container">
<div class="row">
<div id="layer-videos-section">
<?php
$myposts = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-video'
)
)
) );
// Open the loop
if ( $myposts->have_posts() ) : while ( $myposts->have_posts() ) : $myposts->the_post();
$year = mysql2date('Y', $post->post_date);
$month = mysql2date('n', $post->post_date);
$day = mysql2date('j', $post->post_date);
?>
<p>
<span class="the_article">
<?php the_title(); ?>
</span>
<span class="the_day">
<?php the_time('j F Y'); ?>
</span>
</p>
<?php
// Close the loop
endwhile; endif;
// Reset $post data to default query
wp_reset_postdata();
?>
</div>
</div>
I got it!
I installed the plugin Featured Video Plus and got the vídeo url using a native function from this plugin:
get_the_post_video_url( $post_id )
I'm hoping someone can help me with this. I'm new to php so it is very much learning on the job.
I am customising an existing Wordpress theme but I am facing a problem with the themes advanced search/results.
The theme has no way to order search results and I know that the default order for Wordpress is by date. Currently if I perform a search, the results are displayed in date order but I need the results to be price high to low.
The current code is as follows
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article id="post-0" class="post error404 not-found">
<h1 class="posttitle"><?php _e( 'Not Found', THE_LANG ); ?></h1>
<div class="entry">
<p><?php _e( 'Apologies, but no results were found for the requested property archive. Perhaps searching will help find a related post.', THE_LANG ); ?></p>
</div>
</article>
<?php endif; ?>
<div class="nvr-prop-container row">
<?php if( have_posts() ){ ?>
<div class="search-title twelve columns">
<h4><?php _e('Search Result', THE_LANG); ?> (<?php echo $wp_query->post_count; ?>)</h4>
</div>
<?php
$nvr_idnum = 0;
$nvr_typecol = "nvr-prop-col";
$nvr_imgsize = "property-image";
?>
<ul id="nvr-prop-search" class="<?php echo esc_attr( $nvr_typecol ); ?>">
<?php
while ( have_posts() ) : the_post();
$nvr_idnum++;
echo nvr_prop_get_box( $nvr_imgsize, get_the_ID(), 'element columns', $nvr_unit, $nvr_cursymbol, $nvr_curplace );
$nvr_classpf="";
endwhile; // End the loop. Whew.
?>
I then decided to try and sort the results so I created
$sort_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num date',
'order' => 'DESC',
));
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article id="post-0" class="post error404 not-found">
<h1 class="posttitle"><?php _e( 'Not Found', THE_LANG ); ?></h1>
<div class="entry">
<p><?php _e( 'Apologies, but no results were found for the requested property archive. Perhaps searching will help find a related post.', THE_LANG ); ?></p>
</div>
</article>
<?php endif; ?>
<div class="nvr-prop-container row">
<?php if( $sort_properties->have_posts() ){ ?>
<div class="search-title twelve columns">
<h4><?php _e('Search Result', THE_LANG); ?> (<?php echo $wp_query->post_count; ?>)</h4>
</div>
<?php
$nvr_idnum = 0;
$nvr_typecol = "nvr-prop-col";
$nvr_imgsize = "property-image";
?>
<ul id="nvr-prop-search" class="<?php echo esc_attr( $nvr_typecol ); ?>">
<?php
while ( $sort_properties->have_posts() ) : $sort_properties->the_post();
$nvr_idnum++;
echo nvr_prop_get_box( $nvr_imgsize, get_the_ID(), 'element columns', $nvr_unit, $nvr_cursymbol, $nvr_curplace );
$nvr_classpf="";
endwhile; // End the loop. Whew.
?>
Now when I perform a search, the posts are sorted based on price which is fantastic but... now regardless of how I search all of the site posts are now being displayed.
I felt I was so close to finding a solution but I would very much grateful if someone cold advise me with this.
Kind regards
S
Have you tried to order only by the 'meta_val_num' without the 'date' like:
$args = array(
'post_type' => 'product',
'orderby' => 'meta_value_num',
'meta_key' => 'price',
);
$query = new WP_Query( $args );
Code from: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
I think you have to add the Search Parameter and probably use the new meta_query structure:
$search_query = get_search_query();
$sort_properties = new WP_Query(array(
'post_type' => 'properties',
's' => $search_query,
'orderby' => 'meta_value_num',
'meta_query' => array(
'key' => $nvr_initial.'_price',
'value' => $nvr_price,
)
));
I remember having some trouble, not using "meta_query" => array() for multiple conditions.
I've created a shortcode that displays my Staff post. Each post is floating left and there should be four post on each row. Everything is working fine but I need to add a clear div after every four post since the height may vary depending on the content in each post. How do I add the clear div after every four post in my shortcode? My code is below:
/*staff category start*/
add_shortcode( 'staff_category', 'staff_category' );
function staff_category( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'type' => 'staff',
'order' => 'ASC',
'orderby' => 'menu_order',
'posts' => -1,
'staff_category' => '',
'category' => '',
), $atts ) );
// define query parameters based on attributes
$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'stafftype' => $staff_category,
'category_name' => $category,
);
$query = new WP_Query( $options );
// run the loop based on the query
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article class="fourth-col-center staff-profile">
<?php
if(has_post_thumbnail()) { ?>
<div class="staff-img">
<?php the_post_thumbnail( 'staff-thumb' ); ?>
</div>
<?php } else { ?>
<div class="staff-img">
<?php echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/nophoto.jpg" />'; ?>
</div>
<?php } ?>
<h3><?php the_title(); ?></h3>
<div class="loop-post-meta">
<ul>
<li>
<?php
global $post;
$stafftitle_value = get_post_meta($post->ID, "wpstaff_textfield", true);
if($stafftitle_value){ ?>
<?php echo $stafftitle_value ?>
<?php } ?>
</li>
</ul>
</div>
</article>
<?php endwhile;
wp_reset_postdata(); ?>
<div class="cleared"></div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
/*staff category end*/
You could use CSS, something like this should work:
article.staff-profile:nth-child(4n+5) {
clear: both;
}