Event Organiser date in loop - wordpress

I am using WordPress in combination with WP Events Organiser. On my index.php, I want to display events and posts in the same loop, but only categories of "startseite" should be displayed either for the events as well as for the posts. At the moment it is working like this:
<?php global $wp_query;
$args = array(
'post_type' => array('post','event'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => 'startseite',
'field' => 'slug'
),
array(
'taxonomy' => 'event-category',
'terms' => 'startseite',
'field' => 'slug'),
));
query_posts( $args ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
... further on the loop:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="d-1of3" id="post-<?php the_ID(); ?>" <?php post_class( 'cf' ); ?> role="article">
<div class="entry-content">
<h2 class="h2 entry-title"><?php the_title(); ?></h2>
<p class="byline entry-meta vcard">
<?php printf( __( '', 'bonestheme' ).' %1$s',
'<time class="updated entry-time" datetime="' . get_the_time('Y-m-d') . '" itemprop="datePublished">' . get_the_time(get_option('date_format')) . '</time>'
); ?>
</p>
<section class="cf">
<?php the_content(); ?>
<?php echo eo_get_the_start('jS M YY'); ?>
</section>
</div>
<?php the_post_thumbnail( 'large' ); ?>
</article>
<?php endwhile; endif; ?>
Now I would like to display the date of the Event. I have found the following link with an example: http://codex.wp-event-organiser.com/function-eo_get_events.html
But I can't figure out how to change my code just to display the date. AND also it would be great to order the events by the date. Thanks for your help!!

just check the documentation in here.
Basically you add this:
echo eo_get_the_start('jS M YY');
Where you need to show the date.

Related

WP_Query taxonomy terms add one more post

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

How to re-use initial page content loop in WordPress?

I'm working on a site whereby there is a simple template for a certain page in the site.
The page in question is an About Us page.
On the page there is the first main WordPress loop which just spits out the standard content of the main Wysiwyg field.
Underneath that the template has some code which spits out a loop of a custom post type called Staff. This custom post type has information on each staff member and each one is spat out there.
What I have added to the page via the backend is an Advanced Custom Field using ACF Pro. The field is called Our Story (our_story) and I'd like to be able to spit that field out on the template underneath all the staff members.
If I place the following code :
<p><?php the_field('our_story') ?></p>
into the template in the main WordPress loop which is at the start of the template then it shows up fine but I effectively want to start the same loop again but underneath the loops which have spat out the staff members.
If I copy the exact same loop which is at the start of the template to the end of the template then I don't get anything output on the page where that code is. Do I therefore need to reset the code or something or is there an easy way of perhaps caching field content and placing it elsewhere in the template?
Sorry entire template code shown here :
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php the_field('our_story', '710'); ?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
I've adjusted your template to include named WP_Query() loops, and apply wp_reset_postdata() after each custom loop. This should allow the final the_field() call to have access to the current post ID, which is what Advanced custom fields needs in order to fetch the post metadata for the current post.
Behind the scenes, ACF runs (int) get_the_ID(); to get the ID of the current post. If your custom query loops are not reset, this won't work.
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
// Specify the custom query, so you can build a custom loop.
// ---------------------------------------------------------
$staff_query = new WP_Query( $args );
?>
<?php if ($staff_query->have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while ($staff_query->have_posts()): ?>
<?php
// Set up the post to allow use of template tags
// -------------------------------------------------
$staff_query->the_post();
?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$expert_query = new WP_Query( $args );
?>
<?php if ($expert_query->have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while ($expert_query->have_posts()): ?>
<?php
// Set up post
// -------------------------------------------------
$expert_query->the_post();
?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php
// this should work now:
the_field('our_story');
//the_field('our_story', '710');
?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
I think it's easy to get confused with custom loop logic when it's all handled within a template - I prefer to manage post data for custom loops by means of a function or class method which returns an array of post data - you can then loop over this in your template.
Hope this helps.
Resources: https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
it's because you need a post id to get the field. By default it is the current post's id.
try this: <p><?php the_field('our_story', xxx) ?></p> where xxx is the post id of About Us.

Wordpress order custom post type

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.

wp_query by last month + sort posts

I'm using the WordPress plugin 'I recommend this', it allows people to 'like' posts.
The value is stored as a meta key, which we can query to generate a 'most recommended' page.
Plugin -> http://wordpress.org/support/plugin/i-recommend-this
The loop.
<?php
query_posts('&orderby=meta_value&meta_key=_recommended');
if (have_posts()): while (have_posts()) : the_post();
?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
</article>
<?php endwhile; ?>
<?php endif; ?>
This works, it finds the most recommended posts.
Two questions.
How do I limit the posts returned to a date range of say, the last 3 months?
In a similar vein, how could I then have a button for 'Most popular this week' that'd allow users to see a filtered result of posts from the last 7 days?
You could try something like this, this is just a basic example:
$today = getdate();
$threemonths = $today["mon"]-3;
$args = array(
'date_query' => array(
array(
'after' => array(
'year' => $today["year"],
'month' => $threemonths,
'day' => $today["mday"],
),
'before' => array(
'year' => $today["year"],
'month' => $today["mon"],
'day' => $today["mday"],
),
'inclusive' => true,
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
They break it down further in the wordpress documentation:
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
I worked it out. Used a custom filter along with WP_query array.
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-180 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$featuredPosts = new WP_Query( array(
'meta_key'=>'_recommended',
'orderby' => 'meta_value_num',
'order' => DESC
) );
remove_filter( 'posts_where', 'filter_where' ); ?>
<?php if ( $featuredPosts->have_posts() ) : ?>
<?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<div class="figure">
<?php the_post_thumbnail('frontthumb'); ?>
</div>
<div class="fig-cover">
<div class="fig-bottom">
<div class="title-container">
<?php if ( get_post_meta($post->ID, 'Price', true) ) { ?>
<div class="price-container">
<?php echo get_post_meta($post->ID, "Price", true); ?>
</div>
<h2 class="price-title"><?php the_title(); ?> </h2>
<?php } else { ?>
<h2><?php the_title(); ?> </h2>
<?php } ?>
</div> <!-- end div title-container -->
</div>
</div>
<div class="reco">
<?php if( function_exists('dot_irecommendthis') ) dot_irecommendthis(); ?>
</div>
<a class="the-post-link" href="<?php the_permalink(); ?> ">
</a>
</article> <!-- end div post -->
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
Now I just need to figure out how to do live filter on-site...

Adding pagination to custom post loop in page

I have created a custom page template (testimonials-page.php) and in that template I am
loading custom post type 'testimonials' using the following loop:
<?php query_posts(array(
'posts_per_page' => 5,
'post_type' => 'testimonials',
'orderby' => 'post_date',
'paged' => $paged
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
How do I add pagination to that? I installed the WP Paging plugin, and while that plugin works
great when I call the pagination into category.php using:
<p><?php wp_paging(); ?></p>
Inserting the same thing into testimonial-page.php results in broken formatting and links that
404 on me.
Firstly, never EVER use query_posts unless your intention is to modify the default Wordpress Loop.
Instead, switch to WP Query.
Here's something I wrote for a theme I did for a client using all built-in Wordpress functions. It's been working pretty well for me so far, so I'll integrate it into your code as best as I can:
global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'post_type' => 'testimonials',
'orderby' => 'post_date',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php
echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
the_content();
?>
</div>
<?php
endwhile;
echo '
<div id="wp_pagination">
<a class="first page button" href="'.get_pagenum_link(1).'">«</a>
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$query->max_num_pages;$i++)
echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
echo '
<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>
</div>
';
wp_reset_postdata();
endif;
?>
Jan 2018 Edit:
Also consider using paginate_links, since it's also built into Wordpress, and has more robust options and capabilities.
Try this code for custom loop with pagination:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source:
WordPress custom loop with pagination

Resources