Displaying posts from today forward - wordpress

I have a site that was developed by the developer before me and I am the lucky one that gets to try to figure out how to resolve his issues.
I am currently trying to display events from today forward. This is a CPT and has custom fields. I am not sure if I have just looked at this code for to long and am just missing the answer or what but I just cannot see where I am going wrong.
Not only am I not getting ANY results I am getting "Fatal error: Call to a member function format() on a non-object" in my "Dates:" section.
I have read and tried countless possibilities without success. I am hoping that a fresh pair of eyes can help me out.
Here is the code:
<?php
$today = current_time('Ymd');
$args = array(
'post_type' => 'posts_events',
'post_status' => 'publish',
'posts_per_page' => 3,
'meta_query' => array(
array(
'key' => 'start_date',
'compare' => '>=',
'value' => $today,
)
),
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ));
?>
<!-- CONTENT STARTS -->
<div class="wrapper">
<div class="container_16" id="event-list_container">
<div class="col_14 prefix_1">
<?php
$taxonomy = 'sbts';
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
//'show_count' => $show_count,
//'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<ul class="taxonomy-list-links">
<?php wp_list_categories( $args ); ?>
</ul>
</div>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="container_14 prefix_1 suffix_1 event-list">
<?php $background_image = MultiPostThumbnails::get_post_thumbnail_id(get_post_type(), 'event-thumb', $post->ID); $background_image = wp_get_attachment_image_src( $background_image,'event-thumb' ); ?>
<div class="col_5 event-photo" style="background-image:url(<?php echo $background_image[0]; ?>);"></div>
<div class="col_8 prefix_5 event-content">
<h2><?php the_title(); ?><?php if($today > get_field('end_date')) echo '</h2><p>Past Event</p>'; ?>
<?php $firm = get_field('host_firm'); ?>
<h3>Member Host Firm: <span class="event-p"><?php echo $firm[0]->post_title; ?></span></h3>
<h3>Location: <span class="event-p"><?php echo get_field('event_location'); ?></span></h3>
<h3>Dates: <span class="event-p"><?php $date = DateTime::createFromFormat('Ymd', get_field('start_date')); echo $date->format('M j, Y'); ?> - <?php $date = DateTime::createFromFormat('Ymd', get_field('end_date')); echo $date->format('M j, Y'); ?></span></h3>
Event Details
</div>
</div>
<?php endwhile; ?>
<div class="wrapper">
<div class="container_16 nested" id="page-navigation_container">
<div class="col_14 gap prefix_1" id="page-navigation"><?php theme_pagination(); ?></div>
</div>
</div>
<?php endif; ?>
</div>
</div>
<!-- CONTENT STOPS -->
<?php get_footer(); ?>

You need to use a new query with the args you want to pass for the loop:
Check the wp codex
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Related

post per page is not working with sticky post query in wordpress

this is my post loop that i am using
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => 1
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
?>
<article class="cust-arc-post">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<div class="arc-post-header">
<h3><?php the_title(); ?></h3>
<a class="cat" href="javascript:;">Category Title</a>
</div>
<p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
</article>
<?php endwhile;
wp_reset_postdata();
?>
i tried using offset but no luck,
i think something is wrong with my loop
if anyone can help me with this
thanks in advance
All the information you need can be found in the theme handbook
As stated in the codex, this displays just the first sticky post, if none return the last post published:
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
Your problem likely lies in the rsort(); function, because it's reversing the array from highest to lowest.
Try the below code.
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$posts_per_page = 12;
$sticky_count = count($sticky);
if ($sticky_count < $posts_per_page) {
$posts_per_page = $posts_per_page - $sticky_count;
} else {
$posts_per_page = 1;
}
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => $posts_per_page
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<article class="cust-arc-post">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<div class="arc-post-header">
<h3><?php the_title(); ?></h3>
<a class="cat" href="javascript:;">Category Title</a>
</div>
<p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
</article>
<?php endwhile; wp_reset_postdata(); ?>

Display only author posts in author page in Wordpress

What should I do to display only a certain author posts, in my author.php page? I don't mean an specific author (using author ID), but the default page for authors. With the current code, its displaying posts from all the other authors. Should I use a specific query?
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'post',
// 'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $paged
);
// custom query
$recent_posts = new WP_Query($custom_args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<p class="autor"><?php the_author(); ?></p>
<h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
<p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
</div>
<div class="mask">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo '<div class="art_img" style="background: url('. $url.')"></div>';
?>
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
echo '<div class="pagination">';
if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $recent_posts )); }
echo '</div>';
wp_reset_postdata();
?>
You can use get_posts function of wordpress.
Here is reference:
https://codex.wordpress.org/Template_Tags/get_posts
Pass the required authors Id in the argument list.
Hello just add global $current_user; above $paged variable on top of your provided snippet and add 'author' => $current_user->ID, in your array of $custom_args so it will display posts of specific user in author page
Add author__in into your query
Try this
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'post',
'author__in'=> array(2,4,6), //Authors's id's you like to include
// 'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $paged
);

Multiple custom post query is not working

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.

Add clear div after every four post in post loop shortcode

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;
}

How to push a page's custom field data as a value within a template file?

Long story short. I'm trying to expand the functionality of a portfolio template. It currently can only handle one page. I can duplicate the template, relying on custom fields to pull in specific portfilio items (the theme uses custom post types).
So, here's the bit from my portfolio-template.php:
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'ao',
'posts_per_page' => -1
);
So, I want to create a new page, select the portfolio template, then define a custom field on that page that will pass on the meta_value to the portfolio-template.php
So it would look like this instead:
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'PULL_VALUE_FROM_CUSTOM_FIELD',
'posts_per_page' => -1
);
With "PULL_VALUE_FROM_CUSTOM_FIELD" being the bit that I can't figure out. Sorry if my vernacular is code_crude, I'm new at this!
EDIT: Adding in the full code of the template
<?php
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'ao',
'posts_per_page' => -1
);
$portfolio_query = new WP_Query($args);
if( $portfolio_query->have_posts() ) :
echo '<div id="primary" class="hfeed">';
while( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
// styles
$style = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_style', true);
$media_pos = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_media_position', true);
$width = ( $media_pos == 'zilla-media-center' ) ? 940 : 600;
// project url
$portfolio_url = get_post_meta($post->ID, '_zilla_portfolio_project_url', true);
if( !empty($portfolio_url) )
$portfolio_button_copy = get_post_meta($post->ID, '_zilla_portfolio_project_url_copy', true);
// determine which media to display
$portfolio_display_gallery = get_post_meta($post->ID, '_zilla_portfolio_display_gallery', true);
$portfolio_display_video = get_post_meta($post->ID, '_zilla_portfolio_display_video', true);
$portfolio_display_audio = get_post_meta($post->ID, '_zilla_portfolio_display_audio', true);
// grab everything else
$custom_bg = get_post_meta($post->ID, '_zilla_portfolio_display_background', true);
$portfolio_caption = get_post_meta($post->ID, '_zilla_portfolio_caption', true);
?>
<?php zilla_page_before(); ?>
<!--BEGIN .hentry-->
<div <?php post_class( $style . ' ' . $media_pos ) ?> id="post-<?php the_ID(); ?>">
<?php zilla_page_start(); ?>
<div class="hentry-inner">
<!--BEGIN .entry-media -->
<div class="entry-media">
<?php
if( $portfolio_display_gallery == 'on' ) {
$gallery_layout = get_post_meta( $post->ID, '_zilla_gallery_layout', true);
$slideshow = ( $gallery_layout == 'slideshow' ) ? true : false;
$size = ( $media_pos == 'zilla-media-center' ) ? 'portfolio-full' : 'portfolio-index';
zilla_gallery( $post->ID, $size, $slideshow, $slideshow );
}
if( $portfolio_display_video == 'on' ) {
$embed = get_post_meta($post->ID, '_zilla_video_embed_code', true);
if( !empty( $embed ) ) {
echo stripslashes(htmlspecialchars_decode($embed));
} else {
zilla_video( $post->ID, $width );
}
}
if( $portfolio_display_audio == 'on' ) {
zilla_audio( $post->ID, $width );
}
?>
<!--END .entry-media -->
</div>
<!--BEGIN .entry-content -->
<div class="entry-content">
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php if( !empty($portfolio_caption) )
echo "<p class='zilla-caption'>" . stripslashes(htmlspecialchars_decode($portfolio_caption)) . "</p>"; ?>
<?php if( !empty($portfolio_url) && $media_pos == 'zilla-media-center' ) { ?>
<?php echo $portfolio_button_copy; ?>
<?php } ?>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<p><strong>'.__('Pages:', 'zilla').'</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php if( !empty($portfolio_url) && ( $media_pos == 'zilla-media-left' || $media_pos == 'zilla-media-right' ) ) { ?>
<?php echo $portfolio_button_copy; ?>
<?php } ?>
<!--END .entry-content -->
</div>
</div>
<?php zilla_page_end(); ?>
<!--END .hentry-->
</div>
<?php zilla_page_after(); ?>
<?php endwhile; ?>
<!--END #primary .hfeed-->
</div>
<?php else: ?>
<div id="content">
<!--BEGIN #post-0-->
<div id="post-0" <?php post_class(); ?>>
<h2 class="entry-title"><?php _e('Error: No Portfolios Found', 'zilla') ?></h2>
<!--BEGIN .entry-content-->
<div class="entry-content">
<p><?php _e("Sorry, but no portfolios have been created.", "zilla") ?></p>
<!--END .entry-content-->
</div>
<!--END #post-0-->
</div>
</div>
<?php endif; ?>
Before the Loop, you can access the global variable $post, which in case of single.php and page.php will represent that very post type (post/page/cpt) object that will be displayed by the template file.
If not mistaken, when used in category.php, for example, it will grab the first one.
So, in the case in question:
<?php
global $post;
$the_meta = get_post_meta( 'custom_field_name', $post->ID );
if( !$the_meta )
$the_meta = 'your_backup_solution';
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => $the_meta,
'posts_per_page' => -1
);
$portfolio_query = new WP_Query($args);
// et cetera

Resources