WordPress | Date not always appear - wordpress

I try to create a theme. In that theme I have create a custom post type and I quering the WordPress by using the wp_query to get the posts from that post type with the code that following :
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 18
);
$projects = new WP_Query($args);
while($projects->have_posts())
{
$projects->the_post();
?>
<h3><?php the_title(); ?></h3>
<span><?php the_date(); ?></span>
<?php
}
wp_reset_postdata();
the problem is, that while I get the title for all of my posts, I do not get the date for all of my posts. Some posts have the date, other they don't
Any idea for that issue ?

Replace the_date() with the_time() and specify a date format like so:
the_time('l, F j, Y');
Check out the codex article on formatting your date.

Related

Get post publish date inside ACF foreach

In the post loop I have a foreach loop which displays selected team members in the admin(from the "team" custom post type) from an ACF post object.
What I want to achieve is to display the post-publish date inside the foreach.
The problem is that the code echo get_the_date('M d, Y'); shows the date of the "team" custom post type and not the blog post date because is in the foreach.
I tried to pass the post id but it's shows the posts id of the "team" post types and not the actual "post".
How can I show the 'post_type' => 'post' publish post date inside in the foreach?
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DSC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$postAuthors = get_field('select_post_author'); // ACF post object - custom post type - team
if( $postAuthors ): ?>
<?php foreach( array_slice($postAuthors, 0, 1) as $post): ?>
<div class="post-date">
<span>
<?php
echo get_the_date('M d, Y'); // here I want to get the post publish date but it show the publish date of the team post type items
?>
</span>
</div><!-- /.post-date -->
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile;
wp_reset_postdata();
?>
Thanks
So the issue is due to $post being a global variable. I assume ACF stores an array of posts for get_field('select_post_author') then you need to change your foreach statement local variable name, e.g:
<?php foreach( array_slice($postAuthors, 0, 1) as $postAuthor): ?>
<div class="post-date">
<span>
<?php echo get_the_date('M d, Y', $postAuthor->ID); ?>
</span>
</div><!-- /.post-date -->
<?php endforeach; ?>
I solved the issue by passing the post id in the date.
<?php
$pid = get_the_ID(); //get the post id
$postAuthors = get_field('select_post_author');
 if( $postAuthors ): ?>
Pass the post id in the get_the_date.
echo get_the_date('M d, Y',$pid);

Sort Search Results By Post Type

I am trying to figure out how to sort the search results by post type. I asked the question on wordpress stack exchange and someone tried answering but I don't think they understood what I was trying to achieve.
-- https://wordpress.stackexchange.com/questions/72914/search-results-sorted-by-post-types
For example, when a person searches a term they are taking to the page
with the results, all >posts found from all post types are shown but
up top there are the different post types name >links that will sort
the results and show only the respective post type's post. Right now I
have several loops on the search results page for each post type but when I test it, the >loops are all showing the same results even
though each loop has a query for a different post type.
My code for the search page - http://pastebin.com/L9zEw1cn
This is a little above the first loop <?php global $wp_query; $total_results = $wp_query->found_posts; ?> This is the first loop <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post() ?> //My divs// <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>
Then the second loop which is the same for all the others except the post type name -- <?php $args = array( 'post_type' => 'videos', 's' => $s ); ?> <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post() ?> //My divs <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>
How can I fix it so that each loops display the search results for only that post type?
Try using get_posts for the second loop.
$args = array(
'post_type'=> 'videos',
'numberposts' => -1,
's' => $s
);
$videos = get_posts( $args );
foreach( $videos as $post ) : setup_postdata($post);
// My divs
endforeach;
http://codex.wordpress.org/Template_Tags/get_posts
Edit: added 'numberposts' to the arguments.

Display wordpress posts using get_posts - first post shows no date

Hi am using get_posts to grab all posts tagged as 'news' and display them on a given page. I am using the_date() to get the date but what is weird is that the first post shows no date, whereas all posts after this display the date fine. Also I have used this identical code to show posts tagged as 'blogs' on another page, yet they work fine.
Here is the page:
http://appshare.nsdesign7.net/news/
Also here is the other page the same code is used on but works fine:
http://appshare.nsdesign7.net/blog/
<? $pageTitle = wp_title('',false,'');
if ($pageTitle == " News") { ?>
<?php $appsharenewspage = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby'=> 'title', 'category' => 3 );
$postslist = get_posts( $appsharenewspage ); foreach ($postslist as $post) : setup_postdata($post); ?>
<article class="newsstyle">
<span class="imagestyle"><?php the_post_thumbnail(array(120,120)); ?> </span>
<h3><?php the_title(); ?></h3>
<span class="date"><?php the_date(); ?></span>
<?php the_excerpt(); ?>
<div class="clear"></div>
</article>
<?php endforeach; ?>
<?php } ?>
Try using:
echo get_the_date();
Read Special Note on: http://codex.wordpress.org/Template_Tags/the_date
the_date() is restricted to only show the date once per day. So if you have more then one post from the same day, it will only show on one of those posts.
Use the_time instead.
Try using <?php echo get_the_date(); ?> instead.
the_date displays date if the post is published on the same day only.
Reference: http://codex.wordpress.org/Function_Reference/get_the_date
The date only occurs once per day. ie for 2 posts on the same day one will be without the date. It is a WP function. To get what you want replace with the php function get time. try to use the_time() instead of the_date()

Get random post in Wordpress

How do I get a random post in Wordpress?
I would like to display a button on a page that, when pressed, goes to a random post from the blog. I don't want a random post to be displayed on the page, I just want a link that leads to that post.
I tried searching for a code on Google and here at stackoverflow but no success.
Thanks...
UPDATE:
Here is my template code:
<?php /*Template Name: Random*/ ?>
<?php get_header(); ?>
<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>
<div id="main-content-archive">
<div class="grey-text">Random post</div>
<?php $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );?>
<?php if (have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
?>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
create a page template, and use the following code to get a random post:
//Create WordPress Query with 'orderby' set to 'rand' (Random)
$the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
// output the random post
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
then in a page, just use:
see a random post
I found this post which gave me desired results...
Here's a solution copy/pasted from the wpbeginner blog post. No copyright infringement intended.
Just add the following code to the functions.php file:
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
Use mydomain.com/random/ as your href for your button that leads to the random post.
Thanks everyone who contributed for your help...
Cheers!
I find it is more useful to have a URL that will redirect to a random post that you can use as link in sidebar or in menus. If it is a single WP site and even on wp.com it's really easy, for a blog at
http://mygroovywpsite.me/
All you need to do is append it with ?random
http://mygroovywpsite.me/?random
I found this did not work (nor the wp_beginner code above) on subsites in my multisite installation, either approach just loaded the home page. Maybe I had some funky cache issues. The way I do this on many sites is a few more steps w/o plugins.
First make a Page in your site called "Random" / with the slug "random" -- it does not need any content in it
Then create a page-random.php template
<?php
/*
Random Post Picker
Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand'
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
// redirect to the random post
wp_redirect ( get_permalink () );
exit;
}
?>
Then you get the re-direct for any link on your blog ...../random w/o any wrestling with .htaccess
I've done it this way because I've had to modify the query, sometimes for custom post type, sometimes to restrict to category, etc.
I only had one site that was a problem because the hosting suppressed the use of mySQL queries with ORDER BY RAND()
Another Simple solution to display Random Post
1.First a create a custom page template. Name it as random post or a name of your choice!
2.Open the page and remove the default wp loop and Paste the code below
3.To change the no of post change the number ‘1’ to your choice!
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
Check This
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>

Iterate through custom post type by custom taxonomy type? (Ordering wordpress posts by category, or displaying custom post type by taxonomy term)

I want to have a page that shows all posts, separated by category. The idea is to get the categories, and then iterate through all posts for each category. The problem is complicated by the fact that I want to iterate through all posts of a given custom type, using a custom taxonomy as the categories. (Running Wordpress 3)
In my functions.php, my custom post type is registered as "video" and the custom taxonomy as "video_types".
In my custom page template that is supposed to show all videos arranged by category, this is the code that isn't returning any posts (and they're there, I checked):
<?php
$categories = get_categories(array(
'taxonomy' => 'video_types'
));
foreach ($categories as $cat):
?>
<section id="<?php $cat->slug ?>" class="video-category">
<?php
query_posts(array(
'cat' => $cat->cat_ID,
'posts_per_page' => -1
));
?>
<h2><?php single_cat_title(); ?></h2>
<p class="description"><?php echo category_description($cat->cat_ID); ?></p>
<?php while (have_posts()) : the_post(); ?>
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<article class="video">
<h3><?php the_title(); ?></h3>
<p>
<?php the_content() ?>
</p>
</article>
<?php endwhile; ?>
</section>
<?php endforeach; ?>
Jeez, once you figure out that each item of a custom taxonomy is called a term (not immediately obvious in the wordpress docs for the noob), its all much simpler to search for. This solution is easier to understand without all the custom query stuff.
<?php
// A term is an item of a taxonomy (e.g. "Promotional" could be a term for the taxonomy "video_type")
// ...so $categories could be $terms and it would still make sense
$categories = get_terms('taxonomy_name');
foreach( $categories as $category ):
?>
<section class="category-<?php echo $category ?>">
<h2><?php echo $category->name; // Print the cat title ?></h2>
<p class="description"><?php echo $category->description ?></p>
<div class="<?php echo $category->post_type ?>-list">
<?php
//select posts in this category (term), and of a specified content type (post type)
$posts = get_posts(array(
'post_type' => 'custom_post_type_name',
'taxonomy' => $category->taxonomy,
'term' => $category->slug,
'nopaging' => true, // to show all posts in this category, could also use 'numberposts' => -1 instead
));
foreach($posts as $post): // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
// Now you can do things with the post and display it, like so
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<?php
// Getting custom field data example
echo get_post_meta($post->ID, 'field_key', true);
?>
<?php the_content() ?>
</article>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
And then any gaps in understanding can be filled by searching the functions above in the wordpress codex. In the above code, for my specific application, custom_post_type_name would be video, and taxonomy_name would be video_type (or video_types, I forget).
You might try another approach. Try using get_posts to get your posts sorted by your custom taxonomy, set up a variable that is initially an empty string (called $current_cat or something), and with each loop of the results, check the taxonomy and compare it with $current_cat - if different, print out a header for the new category and then the entry, if the same, print out an entry as usual.
A clear issue with your code (I believe) is that you're not properly querying your custom taxonomy. You should be using simply taxonomy_name => 'value' within your query, a custom taxonomy won't be touched by cat in a query.
Let me know if you need more detail.
edit: More detail!
// get a list of categories, in this case your custom taxonomy (your_taxonomy_name)
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'your_taxonomy_name'";
$categories = $wpdb->get_results($querystr, OBJECT);
foreach( $categories as $category ): // begin a loop through those terms (categories in your custom taxonomy)
echo '<div class="category-header"><h3>'.$category->name.'</h3>'; // print the cat title
echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'your_taxonomy_name')).'</p></div>'; // cat description
$posts = get_posts( array( 'your_taxonomy_name' => $category->name, 'post_type' => 'your_post_type' ) ) //select posts in this category, and of a specified content type
foreach($posts as $post) : // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
[ ... ] // do things with your post (display it)
endforeach;
endforeach;
That should do it - and this may be helpful for using get_posts.

Resources