Ajax and WP_Query/tax_query parameter

Your code looks fine, but it’s failing because your custom taxonomy is not getting registered anywhere in the action hooks of an AJAX call. My guess is that you are registering your custom taxonomies per the Taxonomies documentation, which tells you to add it to the “init” action hook. However, “init” doesn’t get called during … Read more

JSON: schedule creation of json file

My idea was to schedule this function, so it will run once every day or so. After some more research I should be able to fix this (using this post). Note that WP_Cron setted to daily is impossible to control exactly at what time the creation happen. If no one visit your site on night … Read more

How to get posts in different language from WPML Plugin [closed]

Assuming $new_lang holds the desired two letter language code (e.g. ‘fr’), you can do this: global $sitepress; $current_lang = $sitepress->get_current_language(); //save current language $sitepress->switch_lang($new_lang); //…run query here; if you use WP_Query or get_posts make sure you set suppress_filters=0 … $sitepress->switch_lang($current_lang); //restore previous language For more info, check http://wpml.org/documentation/support/achieving-wpml-compatibility-for-your-themes-and-plugins/debugging-theme-compatibility/

RSS feed with specific keyword

You can modify the list of posts displayed in feed using pre_get_posts action. And to target only feeds, you can use is_feed conditional tag. So such code can look like this: add_action( ‘pre_get_posts’, function ( $query ) { if ( ! is_admin() && is_feed() && $query->is_main_query() ) { if ( isset($_GET[‘q’]) && trim($_GET[‘q’]) ) { … Read more

Show two random posts from custom post type

You need an orderby argument. $args = array( ‘post_type’ => ‘custom_advert’, ‘posts_per_page’ => 2, ‘orderby’ => ‘rand’ ); That should pull posts in a random order and stop after retrieving the first two, hence two random posts.

Get attachment by slug

An attachment is just a post with the post_status = inherit and the post_type = attachment, so can be queried with WP_Query or get_posts. Note that slug (post_name) is unique per post type. $_header = get_posts(‘post_type=attachment&name=book-site-header&posts_per_page=1&post_status=inherit’); $header = $_header ? array_pop($_header) : null; $header_url = $header ? wp_get_attachment_url($header->ID) : ”; you can also use the … Read more