Why is my WP Query not returning first result’s post meta?

$show_date = get_field(‘show_date’); $show_venue = get_field(‘show_venue’); // print ‘<pre>’; // print_r($show_date); // print ‘</pre>’; $query->the_post(); You are setting the active post AFTER getting the values. Put $query->the_post() always at the top of that while loop, so that the other functions can rely on the global $post variable being set correctly.

Hide if Custom Post Type is empty

hide div tag if Custom Post Type is empty <?php $query = new WP_Query( array( ‘post_type’ => ‘tour’ ) ); $postnum = 0; if ( $query->have_posts() ) : ?> <div id=”tour” class=”block” style=”margin-top: 50% !important;”> <h3 class=”block”>tour > </h3> <div class=”row events”> <?php while ( $query->have_posts() ) : $query->the_post(); ?> <div class=”col-sm-4 text-left event-cell isold”> … Read more

Can I display custom post types in home.php or need page template?

The simple answer is “yes, you can”, but I’m pretty sure it won’t satisfy you 😉 So… How can you achieve this and show only custom posts on home.php? All you need is to use pre_get_posts hook like so: add_action( ‘pre_get_posts’, ‘add_custom_post_types_to_home_query’ ); function add_custom_post_types_to_home_query( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( … Read more

Get Post Types in admin

Custom post types can only be registered on the init hook. So if you’re trying to get the post types before the init hook, you will only ever get the built-in ones. To get custom post types, you need to use a hook after init, or later on init than the custom post types were … Read more

How to remove Trash link from custom post type

<?php function directory_skip_trash($post_id) { if (get_post_type($post_id) == ‘directory’) { // Force delete wp_delete_post( $post_id, true ); } } add_action(‘wp_trash_post’, ‘directory_skip_trash’); another way Adding this snippet to the functions.php of your wordpress theme or your plugin will remove the edit, view, trash and quick edit links that you see when you mouse over a post type … Read more

Custom Post Type single.php template only shows the latest post

You shouldn’t be using custom queries in these templates. The main query (using have_posts();, the_post(); etc. without $loop) is already set up with the correct posts for these templates. Look at the custom query you’re using on the single template: $args = array(‘post_type’ => ‘journal’, ‘showposts’ => 1, ‘post_status’ => ‘publish’, ‘orderby’ => ‘date’, ‘order’ … Read more

Show data from one Custom Post Type in another Custom Post Type

Add a field to your form that allows selection of a machine. You can use get_posts to generate a select element: $args = [ ‘posts_per_page’ => -1, ‘post_type’ => ‘machine’ ]; $machines = get_posts( $args ); if( ! empty( $machines ) ){ echo ‘<select name=”_machine_id”>’; foreach( $machines as $machine ){ echo ‘<option value=”‘ . $machine->ID … Read more

Proper way to display latest 5 posts grouped by post type?

<?php $post_types = array( ‘bmw’ => ‘BMW’, // post type to use in query => post type to show in <h2> ‘audi’ => ‘Audi’ ); foreach( $post_types as $post_type => $post_type_name ): ?> <h2>Latest <?php echo $post_type_name; ?> Posts:</h2> <ul> <?php $args = array( ‘numberposts’ => 5, ‘post_type’ => $post_type ); $recent_posts = wp_get_recent_posts( $args … Read more