WordPress Practical Interview questions – fronted

<?php

//=====================================================================
//	Display posts list
//=====================================================================

$args = array( 
			'post_type' => 'post', 
		);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();	
endwhile;

//=====================================================================
//	Display posts meta
//=====================================================================

echo get_post_meta(get_the_ID(),'meta_key',true); 

//=====================================================================
//	Display sidebar
//=====================================================================

if ( is_active_sidebar( 'sidebar-1' )  ) :
	dynamic_sidebar('sidebar-1');
endif;	

//=====================================================================
//	WordPress Loop
//=====================================================================

if ( have_posts() ) : // check post is avalable or not,if yes then will go for next step
while ( have_posts() ) : // if you had configured your WordPress reading settings to display five posts on the home page then while loop five times and then stop.
the_post(); // This sets up the post and allows us to retrieve any part of the post including the content, the publication date, the author, the category etc.

// the_post function code
function the_post() {
    global $wp_query;
    $wp_query->the_post();
}

if ( have_posts() ) : while ( have_posts() ) : the_post();
	// DO SOMETHING WITH EACH POST THAT WE FOUND
	endwhile; else: 
	// DO SOMETHING IF NOTHING WAS FOUND
endif;

//=====================================================================
//	WordPress WP Query
//=====================================================================

// The retrieved dataset is stored in the $wp_query object.
// List of Query Vars (https://codex.wordpress.org/WordPress_Query_Vars#Query_variables)
// If you use the_post() with your query, you need to run "wp_reset_postdata()" afterwards to have Template Tags use the main query’s current post again.

// Example 1
$query = new WP_Query('cat=14,15 & posts_per_page=3 & order=ASC');
while ( $query->have_posts() ) : $query->the_post();
	// Your code here
endwhile;
wp_reset_postdata(); // Restore original Post Data

// Example 2
// Full list of parameters (https://codex.wordpress.org/Class_Reference/WP_Query#Parameters)
// Methods and properties (https://codex.wordpress.org/Class_Reference/WP_Query#Methods_and_Properties)
$args = array( 'arg_1' => 'val_1', 'arg_2' => 'val_2' );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
	while ( $query->have_posts() ) : $query->the_post(); 
		// Your code here
	endwhile;
	} else {
        // no posts found
	}
wp_reset_postdata();

//=====================================================================
//	WordPress WP Meta Query
//=====================================================================

// To build a query based on a single custom field, we just need one or more of the following arguments:
//(meta_key, meta_value, meta_type, meta_compare)

$args = array( 
		'post_type' => 'my_post_tname',
		
		// For single key and value
		'meta_key'     => 'my_meta_key',
		'meta_value'   => 'value1',	
		
		// For multiple key and value
		'meta_query' => array(
			array(
				'key'     => 'my_meta_key',
				'value'   => array ('value1','value2'),
			),
			array(
				'key'     => '_thumbnail_id',
				'compare'   => 'EXISTS',
		),)
);
$query = new WP_Query( $args );
while ( $loqueryop->have_posts() ) : $query->the_post();
	echo 'Name : '. get_post_meta(get_the_ID(),'my_meta_key',true); 
endwhile;
wp_reset_postdata();	

//=====================================================================
//	WordPress WP Tax Query
//=====================================================================

$args = array( 
		'post_type' => 'my_post_tname',
		'tax_query' => array(
			 array(
				'taxonomy' => 'projects',
				
				// With slug name
				'field' => 'slug',
				'terms' => array('apartment','farm-house'),
				
				// With ID
				'terms' => array('3','5'),
				
				'operator' => 'NOT IN',
				
		)),	
);

//=====================================================================
//	Custom Search form
//=====================================================================

// Copy and pest your default search form code in perticular page and add your custom post type name in below hidden input field. 
<input type="hidden" name="post_type" value="portfolio">

// Change search function globally to search only post, page and portfolio post types
function prefix_limit_post_types_in_search( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array( 'post','page', 'portfolio' ) );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'prefix_limit_post_types_in_search' );




Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: