Can’t sort get_posts by post_mime_type

As told by mrwweb you have to plug into Wp_Query orderby statement. add_filter( ‘posts_orderby’, ‘wpse53783_order_by_post_mime_type’ ); function wpse53783_order_by_post_mime_type( $orderby ) { global $wpdb; $order = substr( $orderby, strpos( $orderby, ‘ ‘ ) ); $orderby = “$wpdb->posts.post_mime_type $order”; return $orderby; } You can apply the filter before the query then remove it with: remove_filter( ‘posts_orderby’, ‘wpse53783_order_by_post_mime_type’ … Read more

Divide the list into two columns (get_posts)

You could do this with the modulo operator. $posts = get_posts($args); $html=”<ul>”; $limit = 5; $i = 1; foreach ($posts as $post) { $html .= ‘<li>’ . $post->post_title . ‘</li>’; if($i % $limit == 0) { $html .= ‘</ul><ul>’; } $i++; } $html .= ‘</ul>’; echo $html;

Select objects from get_posts()

This sounds like more of PHP question than a WordPress one – get_posts() just returns an array of objects; interact with them like you would any other numerical array: $posts = get_posts(); $first_post = $posts[0]; $last_post = array_slice( $posts, -1, 1 ); $everything_but_first_last = array_slice( $posts, 1, -1 ); Check out the complete list of … Read more

Execute PHP function inside the admin area

Hook into the admin_init action… add_action(‘admin_init’, ‘fetch_all_companies’); function fetch_all_companies() { //do something here… } note: What file are you running your fetch_all_companies() function from and where is it located (the path to the file)? Its likely the WordPress environment is not loaded on the destination file which is why your function appears to be undefined.

Custom field to array?

I will assume that you(or users) will enter post ids into a posts custom field, comma separated, eg: 11,13,34,54 OR 11, 13, 34, 54. Then all you need to do is get the custom field value for the loaded post, explode the custom field value by comma(,): and, then you’ll have a nice array to … Read more

get_posts by id not working

I have just found an alternative solution: global $post; $current_post = $post->ID; $array=range(1,$current_post); $args = array( “numberposts’ => 5, ‘offset’ => 0, ‘category’ => $category->term_id, ‘orderby’ => ‘ID’, ‘order’ => ‘DESC’, ‘exclude’ => $current_post, ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘suppress_filters’ => true, ‘post__in’ => $array ); $posts = get_posts($args); I’m not sure why the … Read more

Get posts from current category?

function filter_where_older( $where=”” ) { $date = get_the_time(‘Y-m-d H:i:s’); $where .= ” AND post_date < ‘$date'”; return $where; } function filter_where_newer( $where=”” ) { $date = get_the_time(‘Y-m-d H:i:s’); $where .= ” AND post_date > ‘$date'”; return $where; } $category = get_the_category(); if(!empty($category)) { $cat_id = $category[0]->term_id; add_filter( ‘posts_where’, ‘filter_where_older’ ); // retrieve older posts from … Read more

How to query for a page, get data, then query for child pages of that page

I don’t understand the distinction you are making between ‘sublevel’ query and ‘toplevel’ query, but this is how I pull children for a page. $children = get_children( array( ‘post_parent’ => $servid, ‘post_type’ => ‘page’, ‘post_status’ => ‘publish’, ‘orderby’ => ‘menu_order’, ‘order’ => ‘ASC’ )); ?> Or… $page_children = get_pages( array( ‘child_of’ => $post->ID, ‘post_type’ => … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)