• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

php

Optimize Custom WordPress Queries

November 20, 2012 by Webhead

A couple things that may optimize your custom wordpress queries.  First there is a parameter called ‘fields’ where  you can specify that you only want the ID field back.  Something like below:

$some_query = new WP_Query( array( ‘fields’ => ‘ids’ ) );

An even more significant optimization is by caching your data and using WordPress’ Transient API like so:

<?php

// Get any existing copy of our transient data

if ( false === ( $special_query_results = get_transient( ‘special_query_results’ ) ) ) {

// It wasn’t there, so regenerate the data and save the transient

$special_query_results = new WP_Query( ‘cat=5&order=random&tag=tech&post_meta_key=thumbnail’ );

set_transient( ‘special_query_results’, $special_query_results );

}

// Use the data like you would have normally…

?>

 

sources:

http://www.wpbeginner.com/wp-tutorials/speed-up-your-wordpress-by-caching-custom-queries-using-transients-api/
http://wordpress-hackers.1065353.n5.nabble.com/Using-WP-Query-to-get-posts-ids-td23952.html
http://codex.wordpress.org/Transients_API

keywords: wp_query only get ids, wp_query cache_results

Filed Under: Coding Tagged With: optimize, php, wordpress

Buddypress User Ids

November 16, 2012 by Webhead

Buddypress has several methods to get the user ids.  This post attempts to clarify the differences between these:

  • bp_current_user_id – Exactly the same as bp_displayed_user_id.  This is possibly an old, now renamed, method.
  • bp_loggedin_user_id – The buddypress logged in user id.
  • bp_displayed_user_id – this will return the user id of the user that you are looking at on a page, like someone else’s user profile.
  • current_user_id – This is the wordpress user id.

Filed Under: Coding Tagged With: buddypress, php, wordpress

WordPress – Change Post Counts on edit.php

October 9, 2012 by Webhead

Problem

I wanted to change what posts were displayed to a user when they logged into wordpress and view the Posts page  I could easily alter what posts are shown by using the ‘pre_get_posts’ filter, but the problem was that the number of posts for each status did not change.  You know, at the top of the page it looks like: “All (5) | Published (5)”.  The number 5 did not reflect the number of posts being shown.

Solution

I was hoping to find a filter of some sort on the wp_count_posts function, but there are none.  The second best solution is to rewrite that whole line using the ‘views_edit-post’ filter where “edit-post” is the page id.  A sample of what can be done is in the link below.

 

source:  http://wordpress.stackexchange.com/questions/36117/wp-post-list-tableget-views-have-post-counts-account-for-filters
keywords:  wp_count_posts filter

Filed Under: Coding Tagged With: php, wordpress

Caching With Widgets

September 24, 2012 by Webhead

W3 Total Cache is a great WordPress plugin that will help your Google Page Speed score and will also speed up your website.  The only problem is that it doesn’t clear the cache when widgets are updated.  So for example if you have a custom theme with a widget on your page and you update this widget, changes are not going to be seen.  You’ll have to empty the cache on the W3 Total Cache page.

WPEngine also has a caching system that doesn’t clear the cache upon editing widgets.  This means that every time you update a widget you have to manually clear the cache.  WPEngine is a great WordPress host, but doing something manually isn’t good for clients.

A solution is to add a function to clear the cache every time the widget is updated.  The code below covers W3 Total Cache, WP Super Cache and WPEngine.  You can copy-paste it into your functions.php or just install the Clear Cache For Widgets plugin.

function clear_cache_for_widgets( $instance ) {

// if W3 Total Cache is being used, clear the cache
if ( function_exists( 'w3tc_pgcache_flush' ) ) {
w3tc_pgcache_flush();
}
// if WP Super Cache is being used, clear the cache
else if ( function_exists( 'wp_cache_clean_cache' ) ) {
global $file_prefix;
wp_cache_clean_cache( $file_prefix );
}
else if ( class_exists( 'WpeCommon' ) ) {
WpeCommon::purge_memcached();
WpeCommon::clear_maxcdn_cache();
WpeCommon::purge_varnish_cache();
}

return $instance;
}
add_filter( 'widget_update_callback', 'clear_cache_for_widgets', 10 );

Source: http://scratch99.com/wordpress/development/clearing-cache-when-widget-saved/
http://wordpress.stackexchange.com/questions/25425/page-cache-for-categories-not-updating-with-w3-total-cache

Filed Under: Coding, Plugins Tagged With: google, optimize, php, wordpress, wpengine

Custom Post Type Templates in Plugins

September 12, 2012 by Webhead

Problem

I made a plugin that created a custom post type, but the only way I could customize the single-customposttype.php and archive-customposttype.php is by placing the files into the theme.  It would be nice if everythere could be contained in the plugin and the theme could override if needed.

Solution

WordPress to the rescue… There is a filter called ‘template_include’ that can be used to set the template.  It’s a general filter so you’ll need to check for your post type.

add_filter( 'template_include', 'my_plugin_templates' );
function my_plugin_templates( $template ) {
    $post_types = array( 'project' );

    if ( is_post_type_archive( $post_types ) && ! file_exists( get_stylesheet_directory() . '/archive-project.php' ) )
        $template = 'path/to/list/template/in/plugin/folder.php';
    if ( is_singular( $post_types ) && ! file_exists( get_stylesheet_directory() . '/single-project.php' ) )
        $template = 'path/to/singular/template/in/plugin/folder.php';

    return $template;
}

source: http://wordpress.stackexchange.com/questions/55763/is-it-possible-to-define-a-template-for-a-custom-post-type-within-a-plugin-indep
keywords:  plugin template custom post type


Filed Under: Coding Tagged With: php, wordpress

wp_insert_post post_category problem

September 11, 2012 by Webhead

Problem

I was wp_insert_post to programmatically create a custom post type with a custom category.  Using the following code did not work however.

$new_post = array(
‘post_title’ => ‘some title’,
‘post_content’ => ‘some description’,
‘post_status’ => ‘publish’,
‘post_author’ => $admin_user->ID,
‘post_category’ => array($category->term_id)
);
wp_insert_post($new_post);

Solution

If I read the wordpress codex better and more thoroughly I probably would have seen that there is another special parameter for custom taxonomies.  Instead of using ‘post_category’ I should be using ‘tax_input’ for custom taxonomy categories.  So instead of the ‘post_category’ line, it would look like:

‘tax_input’ => array($category->taxonomy => array($category->cat_name ));

If this doesn’t work, like it didn’t for me because I’m using it in a CRON, the user you are using may not have rights to modify the taxonomy.  in that case, as the docs say, use wp_set_object_terms.  The problem is that wp_insert_post uses current_user_can to detect if the current user can add the taxonomy.  The issue is documented here: http://core.trac.wordpress.org/ticket/19373

 

source: http://wordpress.org/support/topic/problem-with-wp_insert_post-for-custom-post-type-category-is-not-associated
keywords:  wordpress post_category wp_insert_post

Filed Under: Coding Tagged With: php, wordpress

  • « Go to Previous Page
  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Go to page 5
  • Go to page 6
  • Interim pages omitted …
  • Go to page 8
  • Go to Next Page »

Primary Sidebar

Topics

apache apple bootstrap buddypress chrome cloudways cms css debug drupal eCommerce firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery kinsta linux localization mac os x ms sql mysql open source optimize php php 7.2 svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools
  • Uncategorized