Sorting By Custom Posts With Attachments

Surprisingly, WordPress does not make it very straightforward. Even though lots of people have had similar questions on the wordpress.org forums, I believe yours is unique in that you want to keep the order and sort. This is by no means quality code, but does exactly what you’re looking for. class WP_Query_Custom extends WP_Query { … Read more

query user display_name failed in a custom mysql query foreach

You should use all of the goodness of the wpdb object. #Create a new WPDB object for your custom DB $foo_db = new wpdb(‘username’,’password’,’database’,’localhost’); $query = (‘SELECT * FROM custom_table’); #Use the get_results method – You can return OBJECT(default), OBJECT_K, ARRAY_A, ARRAY_N $results = $foo_db->get_results($query, OBJECT); foreach($results as $row){ $user = new WP_User($row->user_id); } You … Read more

Custom category search box for WordPress

In every template file of your theme, even in the header.php you can put <?php $term = null; $btn = __(‘Search’); if ( is_category() ) { $term = get_queried_object(); } ?> <form method=”get” id=”search_form” action=”<?php echo home_url(); ?>”> <div> <input type=”text” value=”” name=”s” id=”s” /> <?php if ($term) { $btn = sprintf( __(‘Search in %s’), … Read more

Tell wordpress to show a single page instead of an archive

Here is a rough filter: add_filter( ‘template_include’, function($template) { global $wp_query; if (1 == $wp_query->found_posts) { global $wp_query; $type = $wp_query->get(‘post_type’) ?: false; $template_type = $type ? ‘single-‘ . $type. ‘.php’ : ‘single.php’; if ( locate_template($template_type) ) { return locate_template($template_type); } elseif ( $type && locate_template(‘single.php’) ) { return locate_template(‘single.php’); } } return $template; } … Read more

Creating arguments from loop for WP_Query meta_query

How about this? /** * Convert query string to meta query. */ function query_string_to_meta_query( $query_string ) { $meta_query = array( ‘relation’ => ‘AND’ ); foreach ( wp_parse_args( $query_string ) as $key => $value ) { $meta_query[] = array( ‘key’ => $key, ‘value’ => $value ); } return array( ‘post_type’ => array( ‘post’, ‘custom’ ), ‘meta_query’ … Read more