Skip to content

Useful Functions and Queries

Imran Sayed edited this page Jul 16, 2021 · 4 revisions

Get Blog Archive page link

  • get_post_type_archive_link( TCC_DEFAULT_POST_SLUG )

WordPress Queries

$args     = [
	'posts_per_page'         => $number,
	'post_type'              => $post_type,
	'no_found_rows'          => true,
	'update_post_meta_cache' => false,
	'update_post_term_cache' => false,
	'category__in'           => [ 1, 2, 3 ],
	'fields'                 => 'ids',
	'meta_key'               => 'featured',
	'meta_value'             => 'yes',
	'meta_query'             => [
		'relation' => 'OR',
		[
			'compare'  => 'NOT EXISTS',
			'meta_key' => 'featured',
		],
		[
			'meta_key'   => 'featured',
			'meta_value' => 'no',
		],
	],
];
$my_query = new \WP_Query( $args );
  • Loop with custom query
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) :
$my_query->the_post();

<article id="post-<?php the_ID(); ?>" class="related-post">
	<?php
	if ( has_post_thumbnail() ) {
		the_post_thumbnail(
			'medium',
			[ 'class' => 'post-img' ]
		);
	} else {
		?>
		<img src="https://via.placeholder.com/520x328?text=Insight" width="520" height="328"
		     loading="lazy" class="post-img"
		     alt="<?php printf( 'article-%s', esc_html( $the_post_id ) ); ?>">
		<?php
	}
	?>
	<div class="related-post-text text-left">
		<?php the_title( '<h4 class="related-post-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h4>' ); ?>
		<?php the_excerpt(); ?>
	</div>
</article>
Clone this wiki locally