Help with WordPress Query

After calling get_recent_posts() (or any other loop query), you typically have to run a foreach or while loop to cycle through the posts. So, you’ll need to change this: <?php $args = array( ‘tag’ => ‘featured’, ‘posts_per_page’ => ‘3’ ); $recent_posts = wp_get_recent_posts( $args ); ?> <!– Your HTML and WordPress template tags here. –> … Read more

Conditional custom field query

I found this: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query I have modified the date to retrieve the last 15 days. It’s untested, but should work. $expireDays=”15″; $querystr = ” SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->posts.post_status=”publish” AND $wpdb->posts.post_type=”post” AND $wpdb->posts.post_date >= ( CURDATE() – INTERVAL $expireDays DAY ) ORDER BY $wpdb->posts.post_date DESC “; $pageposts = $wpdb->get_results($querystr, … Read more

Tricky WP Query

For that kind of complex query, I’d recommend running two queries and then merging the results. First, get an array for: get all posts WHERE ( post_type=”post” AND category = 7 ) Then, get an array for: ( post_type=”case-studies” AND meta_key = ‘homeslide’ AND meta_value = 1 ) Then use array_merge() to combine the arrays … Read more

How to display posts under a specific subcategory of a given category

It should be enough to do this: query_posts( ‘posts_per_page=10&paged=’.$paged.’&post_type=publication&publication_category=’.$subcat ); If you look at your original code you can see why you went wrong by looking at what you are literally asking in your arguments: I would like posts that are in the main category but are in the child category. When what you want … Read more

CRUD operations using WordPress database API

Querying in WordPress is done by using the the wpdb variable. Be sure to always prepare your sql so WP can make it database safe. Example: global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( “SELECT * FROM `{$wpdb->prefix}TABLE_NAME` WHERE `var` = %d”, 1) ); Then you can loop through the data and output it as for example … Read more

Query Posts By Post Publish Date, but sort by Custom Meta Key

As you’ve discovered, meta queries only work with the post meta table, it is indeed looking for a meta key of post_date. You can’t do timespan queries with a simple query, but we can accomplish this with a combination of a query for the meta key, and a posts_where filter directly on the SQL to … Read more

$wpdb query a post type within a specific taxonomy term while ordering posts by custom meta value?

You have a typo in orderby, and meta_value_num is only used as an orderby value, try this: $args = array( ‘post_type’ => ‘event’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘locations’, ‘field’ => ‘id’, ‘terms’ => $location // location term id ) ), ‘meta_key’ => ‘event_date’, // this meta field stores event date in yymmdd format … Read more

Loop to display random posts only if a custom field matches category

Something like this should be the query you want to run, it adds a condition to the page query that requires the value in “meta_field” to equal the current category’s title. global $wp_query; $args = array(‘meta_query’ => array( array( ‘key’ => ‘meta_field’, ‘value’ => single_cat_title(“”, false), ‘compare’ => ‘=’ ) ) ); query_posts(array_merge($wp_query->query, $args)); // … Read more

Add post_type to index.php

It would be something like this (goes in functions.php or elsewhere, but not template): add_action( ‘pre_get_posts’, ‘include_dictionnaire_in_home’ ); function include_dictionnaire_in_home( $query ) { if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) { $query->set( ‘post_type’, array( ‘post’, ‘dictionnaire’ ) ); } } Note that index.php template is not strictly home template. It is catch-all template … Read more

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