code

Search Admin User List on First and Last Names

This code requires WordPress 3.1 and above!

Add this function and hook to your functions.php to enable searching the first_name and last_name fields on the Admin Users page.  Also enables sorting by lastname, firstname using the special ‘byname:’ prefix to the sort text.

By default, the ‘Search Users’ box on the Admin Users page only checks fields present in wp_users.  This function uses the pre_user_query hook to add wp_usermeta rows having the meta_key of either ‘first_name’ or ‘last_name’ to the FROM clause.  Then, it modifies the WHERE clause to add the check for the search term.

Here is the code: Continue reading

Shortcode Function to List Post Titles

The code below can be added to your functions.php to provide a shortcode which will list post titles.

When you add the code to functions.php, you must be careful, because any errors will make your site unusable.  BACK UP your functions.php before adding this, and make sure that you know how to restore from the backup.

If your functions.php file ends with ‘?>’, just paste the entire code on the line following it.  If it does not end in ‘?>’, remove the ‘<?php’ line and the ‘?>’ line from the code below before pasting it in.

Once the code has been added to functions.php with no errors, create a new Page to display your titles.  Enter any content you want on the page.  Where you want the list of titles to show, enter the shortcode as shown in the comments in the code. Continue reading

Sticky Posts For Categories

A number of requests have been posted for a way to use sticky posts when displaying a category. The code below can be used to modify your category.php to use for all categories, or as category-id#.php to use for a given category id.

The code is an example only.  It must be used to modify your code to fit your theme.

Continue reading

Template to List Posts By First Letter Of Title

Several persons have asked for a template to list posts or pages by the first letter of the title. The code below is set for posts, but is easily modified for pages or custom posts. The CSS will need to be modified to fit your own theme.

This template lists a set number of post titles per page.  See this Tip for a template that lists one letter per page.

Here is a screenshot of the first page:

Here is the pastebin link for the code below.
Continue reading

Align Posts in a Row, aka Posts in Columns

If you want to have several posts in a row across the page, you can use code similar to this. This code is an example.  You will need to customize it to your theme.

The important things to note are:

  • the use of $post_counter to create the divs for each row
  • the creation of a div with class=”post_class” to contain each post
  • the use of CSS to set the width of post_row and post_class

You will need to add the CSS to float the posts left, set the width, and probably set some margins.  There is some sample CSS following the code.  Continue reading

Pagination Using paginate_links()

There are often posts on the WP Support forum asking how to paginate a custom query or an array. Here is a link to an article with one solution: Pagination Without Using A Plugin

The code below shows an example of using the built-in WP function paginate_links() for this purpose. Continue reading

List Posts Alphabetically Under First Letter Of Title

Here is sample code to list posts alphabetically under the first letter of the title.

For a complete template, see this post.

Continue reading

Template Parts in WP 3.0

Use template parts in WP 3.0 for repeated blocks of code. In the example below, you would create a file called loop-custom.php which would be called from each section. Continue reading

Preserve Original Query Arguments

Codex Function_Reference query_posts #Preserving_the_Original_Query_Pagination_etc

If you want to override the original arguments, you must merge your argument array last (reverse of what is shown in Codex):

global $wp_query;
query_posts(
   array_merge(
      $wp_query->query,
      array('cat' => 1)
   )
);

List titles of all custom post types

The code below is from a post by MichaelH on the WP Support forum: Continue reading