Stephen Harris
  • Member for 10 years, 3 months
  • Last seen more than a month ago
When to use WP_query(), query_posts() and pre_get_posts
Accepted answer
160 votes

You are right to say: Never use query_posts anymore pre_get_posts pre_get_posts is a filter, for altering any query. It is most often used to alter only the 'main query': add_action('...

View answer
wp_reset_postdata() or wp_reset_query() after a custom loop?
Accepted answer
95 votes

The difference between the two is that wp_reset_query() - ensure that the main query has been reset to the original main query wp_reset_postdata() - ensures that the global $post has been restored to ...

View answer
How to add a shortcode button to the TinyMCE editor?
Accepted answer
70 votes

To add our button to the TinyMCE editor we need to do several things: Add our button to the toolbar Register a TinyMCE plugin Create that TinyMCE plug-in which tells TinyMCE what to do when our ...

View answer
Sort results by name & asc order on Archive.php
Accepted answer
48 votes

The easiest way to do this is to use a hook (the pre_get_posts hook) to change the order. But you should check that the query is one for which you do want to alter the order! (is_archive() or ...

View answer
Can I exclude a post by meta key using pre_get_posts function?
44 votes

I see many people prefer to use pre_get_posts hook instead of query_posts Yay! So pre_get_posts filters a WP_Query object which means anything you could do via query_posts() you can do via $query-&...

View answer
Is there a plugin that provides a stackoverflow style editor for wordpress?
Accepted answer
42 votes

Welcome to WordPress stack exchange! Just yesterday I created WP-Markdown, which enables Markdown on your posts. It also provides the same interface as this website (the toolbar, and live preview) - ...

View answer
Update Option Stored in Multi-Dimensional Array
Accepted answer
37 votes

As far WordPress is concerned - your multi-dimensional array is one option. To update just part of the multi-dimensional array its necessary to retrieve the entire array, alter it accordingly and ...

View answer
How to avoid infinite loop in save_post callback
Accepted answer
37 votes

You can remove the callback from the save_post hook, update the post and then re-add the call back to the hook. The Codex gives an example. add_action('save_post', 'wpse51363_save_post'); function ...

View answer
wp_verify_nonce vs check_admin_referer
Accepted answer
33 votes

I thought that check_admin_referer checked the nonce (it does call wp_verify_nonce, and the referring url. After digging into the core code I realised that it did not do this. Thinking it was a bug I ...

View answer
Using wp_mail with attachments but no attachments received
Accepted answer
31 votes

The $attachment argument for wp_mail takes a file (or array of files) - but the file path has to be fully specified. For example: <?php $attachments = array(WP_CONTENT_DIR . '/uploads/...

View answer
Get the first post term
Accepted answer
29 votes

I'm not sure what you mean by 'first' taxonomy... but, $terms = get_the_terms( $post->ID, 'mytaxonomy' ); returns an array of taxonomy term objects, so $term = array_pop($terms); Would give you ...

View answer
wp_redirect() function is not working
27 votes

Two things wrong here: Don't use $post->guid as an url You must exit() after using wp_redirect() (see the Codex) wp_redirect() does not exit automatically and should almost always be followed ...

View answer
When should add_rewrite_tag() be used?
Accepted answer
25 votes

The fundamental difference is: The add_rewrite_rule() adds a particular rule which is interpreted The add_rewrite_tag() adds a placeholder to use in url structures. This placeholder is then used to ...

View answer
Prevent post from being published if custom fields not filled
21 votes

As m0r7if3r pointed out, there is no way of preventing a post from being published using the save_post hook, since the by the time that hook is fired, the post is already saved. The following, however,...

View answer
How to create live autofill search?
Accepted answer
20 votes

The following uses jQuery UI Autocomplete, which has been included in WordPress since 3.3. (I've borrowed the format from @Rarst :D). It's still not precisely what you're after, but gives you a good ...

View answer
get_terms by custom post type
19 votes

I'm afraid this isn't possible natively (yet?). See this trac: http://core.trac.wordpress.org/ticket/18106 Similarly on the taxonomy admin page the post count reflects all post types. (I'm pretty sure ...

View answer
How to get the post publish date outside the loop?
Accepted answer
18 votes

get_the_date must be used inside the Loop. For outside the loop use get_the_time. $posts = get_posts(array('numberposts'=>-1)); //Get all published posts foreach ($posts as $post){ echo ...

View answer
WP_Query vs get_posts
Accepted answer
18 votes

The difference between get_posts & WP_Query You can view get_posts() as a slimmed down WP_Query. In fact looking at the source: //... prepares query array $r $get_posts = new WP_Query; return $...

View answer
Adding extra authentication field in login page
17 votes

First of all, I would advise against editing the core files as it will be overwritten when you next update WordPress. Also, you should update WordPress, because it will often include security updates....

View answer
In Which Contexts are Plugins Responsible for Data Validation/Sanitization?
16 votes

There are two concepts here: validation - making sure data is valid, i.e. an integer is an integer, a date is a date (in the right format etc). This should be done just before saving the data. ...

View answer
Using widget options 'outside' the widget
16 votes

@JonathonByrd's answer is probably 'best' - certainly you should using get_option if at all possible, since there's no guarantee the option name will stay the same between WordPress versions. ...

View answer
Should I escape wordpress functions like the_title, the_excerpt, the_content
Accepted answer
16 votes

Escaping depends entirely on the context in which you are using the functions. What is safe for displaying inside <h1> tags, is not necessarily safe to display for the value attribute of an ...

View answer
How to create a custom template for a custom taxonomy?
15 votes

Templates See the Template Hiearchy for a more detailed break down of how WordPress chooses the template. For a taxonomy term slug ('monitors' your example) in the taxonomy taxonomy (e.g. 'products')...

View answer
Change language by clicking a button
Accepted answer
15 votes

By far the best (easiest) way is to use the locale filter (inside get_locale()). First set up a quick function for retrieving a different language to use on the locale filter. /** * A function ...

View answer
Do I need a nonce field for every meta box I add to my custom post type admin?
Accepted answer
15 votes

I would recommend so. You do (and should) have your own nonce with which to check the origin of the data and the intent of the user. If you have just one nonce for a metabox - then you run into ...

View answer
Display Menu Name using wp_nav_menu
Accepted answer
15 votes

If you know the menu's slug, then things are easier, otherwise you can use this function to get the menu at a specified location. <?php function wpse45700_get_menu_by_location( $location ) { ...

View answer
Add a script as a dependency to a registered script
14 votes

Digging through https://github.com/WordPress/WordPress/blob/3.5.1/wp-includes/class.wp-dependencies.php all registered scripts are stored in the global $wp_scripts. You can access them directly ...

View answer
add_menu_page permissions - what am I doing wrong?
Accepted answer
14 votes

You want the admin_menu hook, rather than admin_init. Also, you shouldn't use anonymous functions. Instead, use: function wpse51004_add_menu_page() { add_menu_page('Some Page', 'Some Page', '...

View answer
How can I run custom function when post status is changed?
Accepted answer
14 votes

See this Codex page. In general the hook is {old_status}_to_{new_status}. (Untested) but in your case, the hook would be pending_to_draft: add_action('pending_to_draft','wpse45803_pending_to_draft');...

View answer
Change The Title Of a Meta Box
Accepted answer
13 votes

Improved Answer I've decided to revisit this question after realising how unnecesarily hacky it is. The the best solution is to to remove the metabox, and then re-add it, specifying an alternative ...

View answer
1
2 3 4 5
20