Categories

Getting the post thumbnail URL

You can use wp_get_attachment_image_src() to retrieve an object with the post thumbnail information.

include ('wp-blog-header.php');
$post_id = $_REQUEST['id'];
$img_id = get_post_thumbnail_id($post_id, $size = 'post-thumbnail', $attr = '' );
$image = wp_get_attachment_image_src( $img_id );
print_r($image);

While methods such as get_the_post_thumbnail() returns an image tag (), wp_get_attachment_image_src() returns a numeric array with the thumbnail info:

  • $image[0] => url
  • $image[1] => width
  • $image[2] => height

You can also determine if a post has attachments by calling has_post_thumbnail(), which returns a Boolean (true or false)

Getting pages by meta_key value

The following example shows how to query posts that have a custom filed name color and the assigned value is red.

$red_pages  = query_posts('post_type=page&meta_key=color&meta_value=red&orderby=menu_order&order=ASC');

Get page children

The following example requests pages whose parent is ID 123. By specifying depth as 1 we can make sure only inmediata children will be return. A list of all pages can be obtained if depth is set as zero (0).

$params = array(
    'post_type'=>'page',
    'post_parent'=>'123',
    'depth'=>'1',
    'orderby'=>'menu_order',
    'order'=>'ASC'
);
$pages = query_posts($params);
print_r($pages, 'ARRAY_A');

Using WordPress Database Class to access data

WordPress exposes an instance of a class named $wpdb that lets you access any table within your blog database. The following example gets a row from the users table and print the result as an associative array.

<?php
$user_info = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE id = 1", 'ARRAY_A');
print_r($user_info);
?>