Multiple Custom Post Type queries, how to DRY it up

The below example is your code in a loop. The post_types array includes all the post types that you’re querying. Then, the next bit (foreach, more info here) runs the code wrapped in curly braces ({ to }) for each item in that post_types-array. post_types = array(‘recipes’, ‘value_chain’, ‘press’, ‘multimedia’); foreach($post_types as $post_type){ $query = … Read more

Loop stopped working

While <? was once a valid php opener, you should be using <?php. Try changing this line: <h2>Colour Chart</h2> <? to <h2>Colour Chart</h2> <?php That should allow the php to be read correctly.

How to list articles by year based on url?

Avoid using query_posts() Please don’t use query_posts(‘posts_per_page=10’); in your date.php template file as it will override the main query instance and thus affect the main loop. This can truly ruin the day! If you need to modify e.g. the posts_per_page for the main date query, use the pre_get_posts action to modify the query variables, before … Read more

Best choice for multiple loop in page?

get_posts() is just a wrapper for WP_Query, however it does have two main differences: get_posts() just returns an array of posts, so doesn’t provide methods for a Loop. get_posts() sets the ignore_sticky_posts and no_found_rows arguments to true. no_found_rows is required for pagination, and disabling it improves performance. #1 means means that you need to use … Read more

Custom WP_Query id

This is a very unusual way of accomplishing what you wanted but my problem is that the query filter receives via get the ID, With that suppose I have two posts one with the id=10 and another with the id=100 ends up that only the post from id 10 is listed. I do not know … Read more

Loop doesn’t show title of second post and posts thereafter

The code posted for your index.php file contains an error. The WordPress function the_post() is called twice in each iteration. As this function iterates the post index and sets up the conditionals for each post, calling this twice is the likely cause of your problem. Remove the second instance of the_post() immediately after your H2. … Read more

Array ids post to function have_post

If I understand it properly, You can use the post__in parameter to query for specific posts where the post ID is in that parameter: $ids = [ 1, 2, 3, 4, 5 ]; $query = new WP_Query( array( ‘post__in’ => $ids, // … other args, if any. ) ); // Then loop through the posts. … Read more

How can I group posts by months and years?

You can do it like so: global $post; $posts = get_posts( array( ‘post_type’ => ‘press’, ‘nopaging’ => true, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, // it’s DESC; not DSC // There’s no use setting posts_per_page when nopaging is enabled. // Because posts_per_page will be ignored when nopaging is enabled. ) ); $_year_mon = ”; // … Read more