(Wordpress) Only show future posts in archive & search results - wordpress

I have an events category which is searchable by a sub-category (by city where the event is being held). I have the 'The Future is Now!' plugin to allow me to publish future dated posts but hoping that I can set up a paginated template that, when a post 'expires' it will no longer show up in the loop/archive.
I'm also wondering if you can filter out these posts from search results as well?
This is my current Events page code if this helps:
<h2>Upcoming Events</h2>
<ul class="posts">
<?php
$limit = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('showposts=' . $limit . '&paged=' . $paged .'&cat=1&order=ASC');
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="events_ticker" id="post-<?php the_ID(); ?>">
<span class="left">
<h3><?php the_time('F jS, Y') ?></h3>
<p><?php if (strlen($post->post_title) > 25) { echo substr(the_title($before = '', $after = '', FALSE), 0, 25) . '...';} else {the_title();} ?></p>
<?php global $more; $more = 0; the_excerpt(); ?>
<p>Read More</p>
</span>
<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(143,110), array("class" => "right post_thumbnail")); } ?>
</li>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/content_breaker_wide.png" alt=" breaker" class="content_breaker" />
<?php endwhile; ?>
</ul>
<!-- end events loop -->
<div class="navigation">
<p class="alignleft"><?php next_posts_link('< Next') ?></p>
<p class="alignright"><?php previous_posts_link('Next >') ?></p>
</div>
<?php endif; ?>

I don't think there is an easy parameter to say "all posts until this date", so you should implement the posts_where_paged filter. There, you can check for !(is_archive() || is_search()), because these two can continue as normal. For the other parts, you add a " AND $wpdb->posts.post_date < NOW()" or something like that (but don't use now, since that will hide events that happen later today, and you probably don't want that).
A similar question was asked on the WordPress Answers Stack Exchange site (in private beta until Aug 20 2010, so you can't visit it unless you pre-registered). Joe Hoyle's suggestion there was simple:
If all you are wanting to do is add an
extra date for 'show times', it may be
easier to add a meta box which does
exactly that - using the Publish date
to spoof it could be potentially
problematic, as WordPress will set it
to "future" status whenever it is
updated with a future publish date (I
think), so you will have to be hooking
every time a post is updated to set it
back again. Also, that way you could
reserve "Publish Date" for what it is
intended.
I would probably just use a meta_key,
and a custom meta box. Though it
depends how complex your queries are
that show the posts.
If you set the meta_value as a
timestamp, you can order by the show
time date still, or select before /
after a certain date:
$events = get_posts('post_type=events&meta_key=show_time&meta_value='
. strtotime( '+1 week' ) .
'&meta_compare=<&orderby=meta_value');
Would get all "events" with a showtime
no later then a week from the current
date. Note: The above is untested, but
should work :)
(This answer is community wiki so I don't get rep points for just reposting what Joe said.)

Related

I am trying to create custom field to fetch posts by title and display them in a hero grid n my WP website [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am new to WordPress, and coding in general. I am trying to start a news website. Now, I have created a grid hero section for featured posts.
I would like to be able to select which posts are placed in a hero grid.I assume that one of the ways is to fetch them by post_title.I have watched a couple tutorials
and tryed to implement that functionality with ACF plugin but with no success. So far I am only able to display posts in a grid but they are not displayed by my personal preference which is what I would like to achieve. Tnx in advnace. Here is the code:
<section class="page-wrap">
<div class="container-sm">
<h1><?php the_title();?></h1>
<?php get_template_part('includes/section','content');?>
</div>
<?php
$args = array(
'post_type'=>'post',
'posts_per_page' => 3
);
$_posts = new WP_Query($args);
?>
<?php if($_posts->have_posts()):?>
<div class="row mt-5">
<?php while($_posts->have_posts()) : $_posts->the_post();?>
<?php if(has_post_thumbnail()) {
$thumbnail_data = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'caursel' );
$thumbnail_url = $thumbnail_data[0];
}
?>
<div id="post-<?php the_ID(); ?>" style="background-image:url('<?php echo $thumbnail_url ?>');width:440px;margin-left:3px;padding:10px; border-radius:10px;display:flex;align:center;" >
<a href="<?php the_permalink();?>">
<h3><?php the_title();?></h3>
</a>
<h6><?php the_excerpt();?></h6>
</div>
<?php endwhile;?>
</div>
<?php endif;?>
</section>`
If you have tried ACF and you are accustomed with it, I would go for the 'Relationship' custom field.
With this field, you can choose what posts can be chosen (I.e by category, tag, etc), as well as the minimum and maximum posts which can be chosen (in your case, maybe one, since it is a featured post).
Once you have set up this custom field in the back end, you can add a post to the field.
In your text editor, you can access the ACF like so:
<?php
$post_objects = get_field('name_of_acf_relation_field');
if( $post_objects ) {
foreach( $post_objects as $single) {
$ID = $single->ID; ?>
// write html code here and use your $ID to access the post
// you can access other custom fields too by using the $ID
// i.e <?php the_field('field_name', $ID); ?>
<?php } ?>
<?php } ?>

Assign an image to a taxonomy in wordpress?

I am trying to get a different icon to appear next to an event in Wordpress. I am use the "The Event Calendar" plugin and the events are stored as taxonomies. I have three taxonomies. "Tax1", "Tax2" and "Tax3".
I cannot seem to figure out how to call to check what the taxonomies are and assign an icon to each one. Here is what I have so far.
" title="" rel="bookmark">
<?php $taxonomy_exist = taxonomy_exists( 'Tax2' ); ?>
<?php if(is_tax($taxonomy_exist)) { ?>
<p>working!!!!!!!!!!</p>
<?php } else { ?>
<img class="cat-icon" src="/wp-content/uploads/2017/11/Heart.png" />
<?php } ?>
<?php $title = get_the_title($event); ?>
<?php if (strlen($title) > 30) { ?>
<?php echo mb_strimwidth($title, 0, 30, '...'); ?>
<?php } else { ?>
<?php echo $title ?>
<?php }?></a></span><!--.event-title-->
So it is getting to the if statement and failing, showing the heart icon. How can I see if it is part of the "Tax2" taxonomy?
Problem 1:
is_tax() accepts taxonomy names as a first argument. But you put $taxonomy_exist parameter which is boolean. (true/false). Of course it should not work and always return zero. Because there is not any taxonomy called "true" or "false".
Problem 2: Are you sure that tax1, tax2 and tax3 are taxnomies? may be they are terms? if they are terms then i will add a solution code as an update to this answer. If they are taxonomies, then i don't see any logic there. Because taxonomies are not objects to be related to posts. Taxonomy is name of categorization, terms are member elements of those taxonomies.

Final posts not showing on last category page on wordpress site

I have a WordPress site that has a ton of posts on it, all categorized. I set up a new theme, with pagination (15 posts per page), so the user can cycle through each page. Some of the categories paginate fine. Others are missing the final page.
So, if a category has 66 posts ... the first 4 pages show 15 different posts. However, when I click to view page 5, the page says "no posts found". Where did the last 6 posts go? They still show up in my administration (as published and visible). However, other category pages do not have this issue - for example, I have a category with 42 post, and it has 3 page ... the last page of which has 12 of the final post.
So, the pagination seems to be working fine (since it clearly shows the correct number of pages, for the number of posts). Please take a look below at the code I have... this is code from my templated index.php page (I didnt set up a category.php page, because it lists very similarly to the homepage).
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // Page
$args = "posts_per_page=15&paged=".$paged; // Base query
$category = get_queried_object(); // Get Cat info
$thisCat = $category->term_id; // Get Cat ID (if exists)
$tagID = get_queried_object()->term_id; // Get Tag ID (if exists)
echo '<!-- paged: '.$paged.'-->';
echo '<!-- catID: '.$thisCat.'-->';
echo '<!-- tagID: '.$tagID.'-->';
if (is_home() || is_front_page()) { // HOMEPAGE
query_posts($args.'&orderby=rand');
echo '<!-- args: '.$args.'&orderby=rand'.'-->';
} elseif ( is_search() ) { // SEARCH RESULTS ?>
<?php
$search_query = get_search_query();
query_posts($args.'&s='.$search_query);
echo "<!-- args: ".$args.'&s='.$search_query."-->"; ?>
<h1>Search</h1>
<div class="content_labels">
<div class="content_label">SEARCH RESULTS FOR: <?php echo $s; ?></div>
</div>
<div class="clear" style="margin:0 0 10px 0;"></div>
<div class="previouspage">
<< Previous Page
</div><?php
} elseif( is_category() ) { // CATEGORY
query_posts($args.'&cat='.$thisCat);
echo '<!-- args: '.$args.'&cat='.$thisCat.'-->'; ?>
<div class="content_labels">
<div class="content_label">Category:</div>
</div>
<h1><?php single_cat_title( '', true ); ?></h1>
<div class="clear" style="margin:0 0 10px 0;"></div>
<div class="previouspage">
<< Previous Page
</div><?php
} elseif( is_tag()) { // TAGS
echo '<!-- args: '.$args.'&tag_id='.$tagID.'-->';
query_posts($args.'&tag_id='.$tagID); ?>
<div class="content_labels">
<div class="content_label">Tag:</div>
</div>
<h1><?php single_tag_title(); ?> </h1>
<div class="clear" style="margin:0 0 10px 0;"></div>
<div class="previouspage">
Previous Page
</div><?php
}
if ( have_posts() ) :
$i=1;
while ( have_posts() ) : the_post(); ?>
// PAGE CODE GOES HERE
endwhile; ?>
<?php base_pagination(); // PAGINATION CODE ?>
<?php endif; ?>
Here is the pagination code, from my functions.php ... I don't think this is the issue...
function base_pagination() {
global $wp_query;
$big = 999999999; // This needs to be an unlikely integer
// For more options and info view the docs for paginate_links()
// http://codex.wordpress.org/Function_Reference/paginate_links
$paginate_links = paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'mid_size' => 5
) );
// Display the pagination if more than one page is found
if ( $paginate_links ) {
echo '<div class="pagination">';
echo $paginate_links;
echo '</div><!--// end .pagination -->';
}
}
Can anyone see what is going wrong? I've been playing with this for several hours, and I can't seem to find a solution...
So, I did some digging, and tried some things... I couldn't find anything wrong with the code above. The query was correct (showing 15 per page, for each category archive page, etc.), and the pagination was working ... In the end, the default POSTS PER PAGE was conflicting with my own posts_per_page=15 query. Not sure WHERE this conflict was occurring (that is beyond my skills) - but I did learn how to stop it.
Under SETTINGS - READING ...
I just changed the "Blog pages show at most" to "15"
This wasn't an ideal fix (since I don't know where this problem started, and I can't adjust custom "posts_per_page" if it differs from 15)... but my site now works how I want.

Wordpress show posts that start today and end in one year

I would like to know some way to do this, maybe someone can help me out a little bit!
This is the code i have at the moment:
<?php $featured = new WP_Query('showposts=10&cat=23'); while($featured ->have_posts()) : $featured->the_post(); ?>
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?>
<?php $image_id = get_post_thumbnail_id(); $image_url = wp_get_attachment_image_src($image_id,'large'); $image_url = $image_url[0]; ?>
<?php $blogurl = get_bloginfo('url'); $image_url = str_replace($blogurl, '', $image_url); ?>
So straight forward question is how can i display posts that start today and end in one year, from a specific category
Thanks!
See the Time Parameters section in the WordPress Codex page for WP Query, the part about filters should prove useful. Alternatively, see this post on how to hide content based on age of post in WordPress.

WordPress Rewind Posts

I have the following code: http://pastebin.com/ng99augD
It basically spits out five posts for the five authors I have specified in the query posts in each chunk of code and then rewinds the posts prior to the next query. This is far from ideal and would like to make the code more efficient without having to repeat all the template code for the posts over and over. I also want them to be in a random order instead of coming out in the order they have been written as I don't want any particular author to be at the top. Thanks.
I'm not sure why the use of rewind_posts() is necessary. You are creating a new query using by your use of query_posts() each time. rewind_posts() rewinds the query for re-use.
If your performance isn't suffering, it may be okay to run 5 queries to get your five authors. To avoid rewriting the same template code each time you have at least two options. Create a php file with the template code in it, and include it each time you need it:
<?php include('author_posts.php');?>
Or, better yet, create a function in your functions.php or in a plugin file that accepts your query results and outputs the template.
<?php $posts = query_posts('posts_per_page=1&author=author1');
if(function_exists('my_authors')){my_authors($posts);}
?>
Then in your functions.php or plugin:
function my_authors($posts){
//template code here, using $posts->ID, $posts->post_content, etc
}
The third option, which cleans up your code and the number of queries would be to use a category to display posts assigned to it here. You are making 5 queries to display five posts. To use one query to display five posts and sort randomly like you want to, edit each of the five posts and assign them to a new category, lets say:'author sampler'. Then in your template file:
<?php $posts = get_posts('category=author sampler'&order_by=rand&order=asc');
foreach($posts as $post):?>
<?php setup_postdata($post);
//your posted template code follows:
?>
<article id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?><?php edit_post_link('Edit', ' (', ')'); ?></h2>
<p class="p-cat">In: <?php the_category('|') ?></p>
<p class="p-author">
<span class="name">
<?php the_author_posts_link(); ?></span> <span class="avatar"><?php echo get_avatar( $email, $size = '32' ); ?>
</span>
</p>
<?php //etc ?>
<?php endforeach;?>

Resources