Posted in WordPress

WordPress Code Snippets

  • Get only posts with featured images
  • Migrating a WordPress site (SQL)
  • Custom Pagination
  • Twitter-Like time display
  • WP 3.8 custom post type icon
  • Unregister Default Widgets
  • Sort by meta value


Get only posts with featured images

$args = array(
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'order'            => 'DESC',
    'meta_query'       => array(
        array(
            'key'     => '_thumbnail_id',
            'value'   => '',
            'compare' => '!=',
        )
    )
);
$awesome_posts = new WP_Query( $args );

Migrating a WordPress site


UPDATE wp_posts SET guid = replace(guid, 'http://www.old-domain.com','http://www.new-domain.com');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');

UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://www.old-domain.com', 'http://www.new-domain.com');

UPDATE wp_options SET option_value = replace(option_value, 'http://www.old-domain.com', 'http://www.new-domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';

Pagination

// Pagination for a WordPress loop
$list = new WP_Query( $query_args );
$pagination = array(
	'base'       => str_replace( 99999, '%#%', get_pagenum_link( 99999 ) ),
	'format'     => '?paged=%#%',
	'current'    => max( 1, get_query_var( 'paged' ) ),
	'total'      => $list->max_num_pages,
	'next_text'  => 'next',
	'prev_text'  => 'previous'
);
echo '</pre>
<div class="pagination primary-links">' . paginate_links( $pagination ) . '</div>
<pre>
';

// Pagination for anything
$list = range(1, 100);
$items_per_page = 12;
$pagination = array(
	'base'       => get_bloginfo( 'url' ) . '/mypage/%_%',
	'format'     => '?paged=%#%',
	'current'    => $_GET['current_page'],
	'total'      => ceil( max($list) / $items_per_page ),
	'next_text'  => 'go forth',
	'prev_text'  => 'go back'
);
echo '</pre>
<div class="pagination primary-links">' . paginate_links( $pagination ) . '</div>
<pre>
';

Twitter-Like time display

$diff = human_time_diff( $time, current_time('timestamp') );
echo 'This comment was submitted ' . $diff . 'ago';

WP 3.8 custom post type icon
Icon here – Dashicons

'menu_icon'=>'dashicons-format-gallery'

Unregister Default Widgets

<?php 
	function iets_unregister_default_widgets() {
		// WordPress
		unregister_widget('WP_Widget_Pages');
		unregister_widget('WP_Widget_Calendar');
		unregister_widget('WP_Widget_Archives');
		unregister_widget('WP_Widget_Links');
		unregister_widget('WP_Widget_Meta');
		unregister_widget('WP_Widget_Search');
		unregister_widget('WP_Widget_Text');
		unregister_widget('WP_Widget_Categories');
		unregister_widget('WP_Widget_Recent_Posts');
		unregister_widget('WP_Widget_Recent_Comments');
		unregister_widget('WP_Widget_RSS');
		unregister_widget('WP_Widget_Tag_Cloud');
		unregister_widget('WP_Nav_Menu_Widget');

		// bbPress
		unregister_widget('BBP_Login_Widget');
		unregister_widget('BBP_Views_Widget');
		unregister_widget('BBP_Search_Widget');
		unregister_widget('BBP_Forums_Widget');
		unregister_widget('BBP_Topics_Widget');
		unregister_widget('BBP_Stats_Widget');
		unregister_widget('BBP_Replies_Widget');
	}

	add_action('widgets_init', 'iets_unregister_default_widgets', 11);
?>

Sort by meta value

<?php
	    $args_query = array(
	    	'post_type' => 'treatment',
	    	'post_status' => 'publish',
	    	'posts_per_page' => $count,
			'meta_key' => 'treatments_views',
			'orderby' => 'meta_value_num',
			'order' => 'DESC',
	    );

		$top_treatments = new WP_Query( $args_query );
?>

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