Limit WP_Query to only X results (total, not per page)

As you already mentioned, to limit the number of posts retrieved by the query, you can use posts_per_page. This argument works both for get_posts and WP_Query(), for example: $args = array( ‘posts_per_page’ => 5, ); $posts_array = get_posts( $args ); And as stated in the link you provided, no_found_rows = true will end the query … Read more

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.

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