Query Posts but only return 1 from a custom taxonomy?

I think that using standard WP_Query you should run 6 different loops, but this is overwhelming. But using a custom $wpdb query you can do the trick… function get_unique_posts_tax( $taxonomy = ‘category’, $number = 10, $cpt=”post”) { global $wpdb; return $wpdb->get_results( $wpdb->prepare( “SELECT p.* FROM $wpdb->term_relationships JOIN $wpdb->term_taxonomy AS tt ON tt.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id JOIN … Read more

Shortcode returns escaped HTML tags

I would recommend using single quotes if you want to include some HTML, also you need to return something otherwise nothing happens. // WP Shortcode function text_shortcode() { return ‘<strong>bold text:</strong> <a href=”https://wordpress.stackexchange.com/questions/318934/shortcode-returns-escaped-html-tags”>See wordpress.stackexchange.com</a>’; } add_shortcode(‘bold-text’, ‘text_shortcode’); Your WordPress shortcode would be: [bold-text] Edit: You don’t need html_entity_decode or htmlentities only when you’re doing something … Read more

How to order a post type with meta_value_num and if meta_value_num does not exist then order by date

You can pass a space-delimited set of columns to orderby argument of WP Query: $args = array( ‘meta_key’ => ‘wp_ratings’, ‘orderby’ => ‘meta_value_num date’ ); $query = new WP_Query( $args ); You can also pass an array of key => sort_order for more granular control, for example: $args = array( ‘meta_key’ => ‘wp_ratings’, ‘orderby’ => … Read more

Best practice for multiple queries on page

WP_Query can handle all of these cases. See the section on handling multiple taxonomies. $this_cat=”interviews”; $this_tag = ‘angelina-jolie’; $args = array( ‘tax_query’ => array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘category’, ‘field’ => ‘slug’, ‘terms’ => $this_cat ), array( ‘taxonomy’ => ‘post_tag’, ‘field’ => ‘slug’, ‘terms’ => $this_tag ) ) ); $interviews = new WP_Query( … Read more

How to pass custom parameter to WP_Query for filtering in pre_get_posts

You can access query variables (including custom ones) via the WP_Query::get() method. For example: $my_query = new WP_Query( array( … ‘wpse105219_custom_var’ => ‘foobar’, … ) ); To ‘catch’ this at pre_get_posts: add_action( ‘pre_get_posts’, ‘wpse105219_pre_get_posts’, 10 ); function wpse105219_pre_get_posts( $query ){ if( $query->get( ‘wpse105219_custom_var’ ) == ‘foobar’ ){ //Alter $query } }

WP API to fetch all the media/posts records if count is more than 100

Add a page number query to your request URL, and continue querying next page numbers until you get an empty result: <your_wordpress_install_base_url>/wp-json/wp/v2/media/?per_page=100&page=2 Mark’s suggestion to consider requesting less than 100 per page may be a good one. So for example you may want to do your query this way: <your_wordpress_install_base_url>/wp-json/wp/v2/media/?per_page=25&page=3 Note also that (for me … Read more

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