Custom WordPress Query that should have everything you need
Has all the Query arguments. Just delete the ones you don’t need. Has everything set up but kept the formatting to a minimum. I will probably copy and paste this string of code a million times by the end of my career.
<?
$call_args = array(
'post_type' => $post_type,
'p' => $post_id,
'page_id' => $page_id,
'pagename' => $pagename,
'name' => $post_name,
'year' => $year,
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'posts_per_page' => $posts_per_page,
'author' => $author_id,
'author_name' => $author_name,
'author__in' => array( 37, 47 ),
'author__not_in' => array( 37, 47 ),
'category_name' => $category_slug,
'cat' => $category_id,
'category__and' => array( 37, 47 ),
'category__in' => array( 37, 47 ),
'category__not_in' => array( 37, 47 ),
'tag' => $tag_slug,
'tag_id' => $tag_id,
'tag__and' => array( 37, 47 ),
'tag__in' => array( 37, 47 ),
'tag__not_in' => array( 37, 47 ),
'tag_slug__and' => array( 37, 47 ),
'tag_slug__in' => array( 37, 47 ),
'post_parent' => $parent_id,
'offset' => 1,
'orderby' => '$meta_value',
'order' => 'DESC'
);
$args['tax_query'] = array(
array(
'taxonomy' => 'project_categories',
'field' => 'term_taxonomy_id',
'terms' => $this_cat,
),
);
$call_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query( $call_args );
$count = 0;
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
function string_limit_words($string, $word_limit) { //607AM
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words);
} //607AM
function is_local_link($the_link) {
if (strpos(" $the_link ", site_url())) {
return true;
}else{ return false; }
}
if ( $custom_query->have_posts() ) { while ( $custom_query->have_posts() ) { $custom_query->the_post();
$this_post_id=$post->ID;
$this_post_title= get_the_title();
$this_post_excerpt= get_the_excerpt();
$this_post_excerpt= string_limit_words($this_post_excerpt,25);
$this_post_excerpt .= 'more ->';
$this_post_content= get_the_content();
$this_post_link = get_permalink($this_post_id);
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
//$meta_values = get_post_meta( $post->ID, $key, true );
if (is_local_link($this_post_link)) { $target='_self'; }else{ $target='_blank'; }
?>
<? $count++; ?>
<a target=<? echo $target; ?> href="<? echo $this_post_link; ?>">
<div class="single_post_wrap" id="single_post_<? echo $count; ?>">
<? if (!empty($featured_image[0])) { ?>
<div class="featured_wrap">
<img class="featured_image" id="featured_image_<? echo $count; ?>" src="<? echo $featured_image[0]; ?>" />
</div><!--featured_wrap-->
<? } ?>
<h1 class="post_title"><? the_title(); ?></h1>
<div class="post_content"><? the_content(); ?></div>
</div><!--single_post_wrap-->
</a>
<?php } } ?>
<?php
the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => __( 'Back', 'textdomain' ),
'next_text' => __( 'Forward', 'textdomain' ),
) );
$wp_query = NULL;
$wp_query = $temp_query;
wp_reset_query();
?>