wp_query to find posts by year and month

Solved by creating custom sql queries. Request for years: SELECT count(*), $wpdb->posts.id as post_id_ts, YEAR( (SELECT $wpdb->postmeta.meta_value FROM $wpdb->postmeta WHERE $wpdb->postmeta.post_id = post_id_ts AND $wpdb->postmeta.meta_key = ‘docs_pubdate’)) AS years FROM $wpdb->posts WHERE $wpdb->posts.post_type=”document_base” AND $wpdb->posts.post_status=”publish” GROUP BY years which will return result like this: Array ( [0] => Array ( [count(*)] => 1 [post_id_ts] => … Read more

Change post featured image on hover

You can add a custom field to you team page, call it “second_picture”, or whatever you want, and add the code <div class=”box”> <div class=”rounded-circle” style=”background-image:url(‘<?php the_post_thumbnail_url(‘full’); ?>’)” id=”staff-pic”> <div class=”second-picture” style=”background-image:url(<?php the_field(‘second_picture’); ?>)”></div> </div> <h3 class=”team-name”><?php the_title(); ?></h3> <p class=”description text-center”><?php echo get_the_content(); ?></p> <div class=”social”><a href=”#”><i class=”fab fa-facebook”></i></a><a href=”#”><i class=”fab fa-twitter”></i></a><a href=”#”><i class=”fab … Read more

Pull Two Different Post Types into Different HTML Containers on the Same Page

Updated 10 hours later: enriched with alternative solution and complemented with comments. <?php /* ******* Built-in post type “Post” ******* */ $arguments_for_posts = array( ‘numberposts’ => 1, // number of posts to get ‘post_type’ => ‘post’ // post type ); // get the array of post objects $posts = get_posts( $arguments_for_posts ); // iterate through … Read more

tax_query returning all posts instead of selective posts in WP_Query

Taxonomy Parameters Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays). This construct allows you to query multiple taxonomies by using the relation parameter in the first (outer) array to describe the boolean relationship between the taxonomy arrays. In ‘tax_query’ you should add array of arrays (see … Read more

Show Two custom Post type and their posts on category page

First, I would recommend making this change in Category.php file, as archive.php is affecting tags as well as other archives according to WordPress Theme Hierarchy file system. Try adding the two queries like next: $cats = get_the_category(); $cat_id = $cats[0]->term_id; // news $args = array( ‘post_type’ => ‘news’, ‘posts_per_page’ => 5, ‘cat’ => $cat_id, ); … Read more

Rewrite URL for only archive page (custom post type)

You can have a separate slug for the post type archive by setting the slug as the value for the has_archive argument, instead of just true: register_post_type( ‘event’, array( ‘has_archive’ => ‘agenda’, // etc. ) ); Now you can use single-event.php and archive-event.php for the single and archive views, but the URL for the archive … Read more

How to search for many post types?

This is documented here: https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters So to search for several post types you do: $args = array( ‘post_type’ => array( ‘post’, ‘page’, ‘movie’, ‘book’ ) ); $query = new WP_Query( $args );

WordPress posts with multiple fields

Unless you’re eager to program the functionality yourself, I would highly recommend “Advanced Custom Fields” (a plugin). It’s very intuitive, and gives you the ability to add custom fields (duh!) to every post-type in your theme. This means that I could add a “Height”-field above my WYSIWIG-editor in my Post-editor.