Wordpress - Force most recent posts first - wordpress

I want to use the plugin WP Smart Sort to sort my posts alphabetically, except I need the most recent posted first on the home page. The relevant existing code for the home page is below. Is there something I can add to it to force it to include the recent posts and go by date rather than by the alphabetical order I want everywhere else?
(In case it helps to see the context, the site is at http://mormonscholarstestify.org/)
<div id="homebox">
<?php if ($aOptions['homebox-id'] != '') { query_posts('showposts=5&cat=' . $aOptions['homebox-id']); } ?>
<?php if(have_posts()) : the_post() ?>
<div id="boxmain">
<img src="<?php $key="thumbnail"; echo get_post_meta($post->ID, $key, true); ?>" alt="<?php the_title() ?>" />
<h3><?php the_title() ?></h3>
<?php $key="affiliation"; echo get_post_meta($post->ID, $key, true); ?><p>
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
<?php while(have_posts()) : the_post() ?>
<div class="boxitem">
<img src="<?php $key="thumbnail"; echo get_post_meta($post->ID, $key, true); ?>" alt="<?php the_title() ?>" />
<h3><?php the_title() ?></h3>
<?php $key="affiliation"; echo get_post_meta($post->ID, $key, true); ?>
</div>
<?php endwhile; ?>
</div>

According to the Wordpress query_posts documentation you should be able to supply an orderby parameter to the querystring to change how the posts are ordered, like so:
query_posts('showposts=5&orderby=date&order=ASC&cat=' . $aOptions['homebox-id']);

Related

Wordpress post object getting page title rather than object title

I have an ACF repeater, which repeats post objects. I am changing the postdata to the post object rather than the page so that i can get the title and thumbnail image. This works perfectly for the first one, however the subsequent objects pull the correct thumbnail but the title is pulled from the page title.
Heres the code:
<?php if( have_rows('service_repeater') ): ?>
<?php while ( have_rows('service_repeater') ) : the_row(); ?>
<?php $post_object = get_sub_field('service'); ?>
<?php if( $post_object ): ?>
<?php $post = $post_object; ?>
<?php setup_postdata( $post ); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<a href="<?php the_permalink(); ?>" class="service">
<div class="background" style="background-image: url('<?php echo $url; ?>');"></div>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_field('read_more_text'); ?></p>
</div>
</a>
<?php unset($post_object, $post); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
Any help would be greatly appreciated!
Ok, so i removed the <?php unset($post_object, $post); ?> and it works now. Im concerned that this will give me trouble later down the line with variables however.

Wordpress post order when using $count to display posts in columns

I’m using this code to display wordpress posts in divs with specific classes. Is there a way to get the posts in a random order and still do this? I’ve searched and can’t find anything that has helped me figure this out. Thanks.
<?php $count = 0; if (have_posts()) : while(have_posts()) : the_post() ?>
<div class="grid4 noall
<?php if( $count%3 == 0 ) { echo 'first'; }; if( $count%3 == 1 ) { echo 'mid'; }; if( $count%3 == 2 ) { echo 'last'; }; $count++; ?>">
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<a class="plink" href="<?php the_permalink() ?>" rel="bookmark" title=" <?php the_title_attribute(); ?>">
<section class="portfolio-wrapper">
<?php the_post_thumbnail( 'thumb-800' ); ?>
<div class="portfolio-title">
<h3 class="portfolio-header"><?php the_title(); ?></h3>
<?php if( get_field('portfolio_subtitle') ): ?>
<h5 class="portfolio-category">
<?php the_field('portfolio_subtitle'); ?>
</h5>
<?php endif; ?>
</div>
</section>
</a>
</article>
</div>
<?php endwhile; ?>
Is this in the main WordPress loop, or are you running a custom query with WP_Query? If the latter, you could just simply pass the 'orderby' => 'rand' parameter in your arguments array. If it's in the main WordPress loop, the answer's slightly different, but similar. Essentially, you'd want to apply the set_query_var function to the main loop, conditionally depending on which page / view you're working with.
https://codex.wordpress.org/Function_Reference/set_query_var
for instance,
if(is_post_type_archive('products')){
set_query_var('orderby','rand');
}

the_post_thumbnail not working

I'm not super familiar with wordpress, but I've been working on getting my layout written as a theme so I can use the wordpress platform. For some reason I can't get the_post_thumbnail function working. I already added
add_theme_support( 'post-thumbnails' );
to my functions.php file and yes I did set a featured image and include the_post_thumbnail(); in the loop.
What am I doing wrong? I went as far as to go through the actual function and found that it is having problems with the $image variable in the wp_get_attachment_image() function while referencing the wp_get_attachment_image_src() function. $image is empty, when I'm guessing it shouldn't be. It get's the post_thumbnail id just fine though so I know it knows there's an image set. It just won't display the darn thing.
I'm writing myself a custom theme from scratch so functions.php only has add_theme_support( 'post-thumbnails' ); in it right now if you're curious.
Edit:
Here's my loop:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="home-entry clearfix" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" >
<?php
if (has_post_thumbnail()) {
the_post_thumbnail();
} else {
echo '<img class="home-thumb trans-border" src="' . catch_first_image() . '" width="200px" height="150px" title="' . the_title() . '" />';
}
?>
</a>
<div class="home-post">
<div class="home-meta">Posted <?php the_time('M j, Y'); ?> - <?php the_category(' , ') ?> - <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></div>
<h2 class="post-title">
<?php the_title(); ?>
<a class="read" href="<?php the_permalink(); ?>" title="Read More">Read More</a>
</h2>
<div class="home-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php echo paginate_links() ?>
<?php else : ?>
<h2>Nothing Found</h2>
<?php endif; ?>
Continued:
So I went and found a theme with featured image support and copied that part of the loop exactly and still nothing:
<?php if(has_post_thumbnail()) {
echo '<span class="thumbnail">';the_post_thumbnail(array(100,100)); echo '</span>';
} else {
$image = evlget_first_image();
if ($image):
echo '<span class="thumbnail"><img src="'.$image.'" alt="';the_title();echo'" /></span>';
endif;
} ?>
So what the heck could it be? I'm so confused...
This function will echo itself.
How to solve: Remove the echo in front of the function.
check this
Are your images locally stored on your server/local machine?
If not, the_post_thumbnail() won't work as it can't retrieve an image based on a URL.

Loop in Sidebar of Wordpress not working on search page with no results

I am using an if elseif statement to check what page it is, and it requires that there are posts on the page so when a search is returned with a result of 0, my code stops working. Any ideas how to code this better?
this code is in my sidebar and is showing the recent articles, you can see an example of a search working here(the sidebar is setup as a sub-footer):
http://ivry.sweetyams.ca/?s=new
and a search not working here:
http://ivry.sweetyams.ca/?s=asjdfkl%3B
Code I am using:
(I have tried putting stuff into the else{ code and it doesn't work either because there are no posts on the 'nothing found' search page
<?php if (have_posts()) : ?>
<?php /* IF SEARCH PAGE */ if (is_search() ) { ?>
<?php query_posts('category_name=0&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php /* IF ESCALADE PAGE */ }elseif (is_category_or_sub(6)) { ?>
<?php query_posts('category_name=escalade&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php /* IF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
<?php query_posts('category_name=montagne&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php } else { ?>
DO SOMETHING ELSE
<?php }?>
<?php endif; ?>`
I am trying to get all recent articles for my sidebar and when there is no result in the search, it also stops my sidebar from working.
The code I added is not in my search page but in my SIDEBAR, it separates the comments into 2 categories, (6 and 14) I am essentially splitting my site using categories for navigation, anything as a child below 6 will be styled one way and 14 the other, you can see the difference on the escalade and montagne links:
This is getting every post below escalade, OR every post below the montagne category
My search pulls results from ALL categories, but if there is no search result, for some reason my sidebar code, the code I included, doesn't work.
first, showposts is deprecited since v2.1, used posts_per_page instead.
second, after each query_post, you need to reset the query like this:
<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
this is how I understand it. see more detail here:query_posts(), hope this helps.
I found a way to get it to work,
I found that it is encouraged to use WP_Query for secondary posts within a page, http://new2wp.com/noob/query_posts-wp_query-differences/
also, for anyone that is wondering the is_category_or_sub function is from here: http://valendesigns.com/wordpress/is-category-or-subcategory-wp-function/
This code checks whether you are on a category or subcategory,
<?php
$my_posts = new WP_Query(); ?>
<?php /* IF ESCALADE PAGE */ if (is_category_or_sub(6)) { ?>
<?php $my_posts->query('posts_per_page=5&cat=6');?>
<?php /* ELSEIF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
<?php $my_posts->query('posts_per_page=5&cat=14'); ?>
<?php } /* AND OTHER PAGE */ else { ?>
<?php $my_posts->query('posts_per_page=5'); ?>
<?php }?>
<?php while ($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts ?>
<a href="<?php echo esc_url( get_permalink( $post->the_permalink ) ) ?>" title="Permanent Link to <?php echo substr($post->post_title,0,200);?>">
<p><?php echo substr($post->post_title,0,200); ?></p>
</a>
<?php endwhile; ?>
<?php wp_reset_query() // RESET QUERY ?>

Getting post id in WP_Query

I am trying to execute the query in WordPress to retrieve the last post from featured category, then check for its custom field and show an image stored in it.
Well, my code is not working for some reason.Can you spot any error there? Or point me in the right direction?
<?php $featured = new WP_Query('showposts=1&category_name=featured'); ?>
<?php if($featured->have_posts()) : ?>
<?php while($featured->have_posts()) : $featured->the_post(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, 'image3', true); ?>"></a>
<?php endwhile; ?>
<?php endif; ?>
Many thanks!
I hope this will do what you expecting
$args['category_name']='featured'; // posts from particular category
$args['orderby']='date'; //to get latest post
$args['order']='DESC';
$args['numberposts']='1';// to get only one post from post list
$featured = new WP_Query($args);

Resources