How can I use the WordPress Loop and Pagination in multiple instances but different scenarios throughout my site?

In your main loop file, check the $args. It filters the query. you can use the same query on other pages using your own options. suppose, on another page, you want to fetch the posts of a different category. your query parameters may be like: $args = array( ‘post_type’ => ‘post’, ‘cat’ => ‘DIFFERENT_CATEGORY’, ‘posts_per_page’ … Read more

How do I display the latest posts from three different categories?

Don’t use query_posts – it’s can really mess things up. Instead, use three WP_Query instances: $post_from_cat_a = new WP_Query(array( ‘category__name’ => array(‘a’), //Get posts from category a ‘posts_per_page’=> 1 //Limit it to the latest one )); if( $post_from_cat_a->have_posts() ){ while( $post_from_cat_a->have_posts() ): $post_from_cat_a->the_post(); //Display output here for post from category a endwhile; } $post_from_cat_b = … Read more

Can’t query by meta_key

Thanks for @dunc and @helgatheviking I got the answer. Here’s my code. You need the post type declared, otherwise it resorts to “post”. I also couldn’t do this unless I put meta_key and meta_value in a ‘meta_query’ multidimensional array. $args = array( ‘post_type’ => ‘tsa_events’, ‘meta_query’ => array( array( ‘key’ => ‘slideshow_image’, ‘value’ => array(”), … Read more

Add Blog to WP homepage Manually

It looks like the theme is widgetized, and it actually has listed as a feature “Blog integration” – see here. Are you sure in the theme settings there are not options related to showing blog posts in one of the widget areas? The other place to look is “Appearance » Widgets”. There may be a … Read more

How can I prevent posts with no title showing up in my query?

Please find below a quick solution to your problem first query for all empty titles place the result in an array query again for post__not_in see below sample code $query = new WP_Query(‘post_title=\’\”); $a_empty_titles = array(); while($query->have_posts()){ $query->the_post(); array_push( $a_empty_titles ,$query->post->ID); } wp_reset_postdata(); wp_reset_query(); echo count($a_empty_titles); $query = new WP_Query( array( ‘post__not_in’ => $a_empty_titles ) … Read more

custom post on homepage

You can also use a new query (with php enabled) so show a single post by ID, i.e. the post with ID 101: <?php $my_query = new WP_Query(‘p=101’); ?> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <a href=”https://wordpress.stackexchange.com/questions/64210/<?php the_permalink() ?>” title=”<?php the_title(); ?>”> <?php the_title(); ?><?php the_content(); ?></a> <?php endwhile; ?> This can be used multiple … Read more

sort and display posts by custom field (wp-query and the loop))

I believe the meta_query part should be a nested array, so your query should be like this: $query = new WP_Query( ‘post_type’ => ‘post’, ‘orderby’ => ‘meta_value’, ‘meta_key’ => ‘ENDS’, ‘order’ => ‘ASC’, ‘posts_per_page’ => -1, ‘meta_query’ => array( array ( ‘key’ => ‘ENDS’, ‘value’ => date(‘Y-m-d H:i:s’), // now ‘compare’ => ‘>=’, ‘type’ => … Read more

How to add orderby parameter for sorting on category.php

You can use the pre_get_posts action hook to set the order on your category archive like so: add_action(‘pre_get_posts’, ‘filter_category_orderby’); function filter_category_orderby( $query ){ if( $query->is_category()){ $query->set(‘orderby’, ‘title’); } } just paste this snippet in your theme’s functions.php and you should be fine.

How to do paging in the loop?

Try adding ‘paged’ => get_query_var( ‘paged’ ) ? get_query_var( ‘paged’ ) : 1, // this will get the right posts for the current page (i.e. if the user loads www.domain.com/blog/page/2 directly) ‘posts_per_page’ => ‘6’ to your $args and delete showposts showpostsis deprecated. From the Codex reference for WP_Query: showposts (int) – number of posts to … Read more

Trying to edit archive.php to only show post extract, with featured image

To replace the full content with the excerpt, change <?php the_content( ‘<em>Continue reading &rarr;</em>’ ); ?> To <?php the_excerpt(); ?> To display the featured image use this: <?php the_post_thumbnail( ‘single-post-thumbnail’ ); ?> Note: Make sure you have this next snippet somewhere in your theme’s functions.php file //add theme support for post thumbnails add_theme_support( ‘post-thumbnails’ );