Can't check if current post is latest (CPT issue)
Support › MB Custom Post Type › Can't check if current post is latest (CPT issue)Resolved
- This topic has 2 replies, 2 voices, and was last updated 1 year, 11 months ago by
Val.
-
AuthorPosts
-
May 16, 2023 at 1:46 AM #41827
Val
ParticipantHello, I'm trying to insert conditional css in wp_head by checking whether a post from a CPT is the most recent one (the latest).
I've tried many different code snippets and none of them worked. Trying to achieve this for regular posts worked, the issue is with MB CPTs. Here's the latest code snippet I'm trying with :
$current_post_id = get_the_ID(); $args = array( 'post_type' => 'projects', 'posts_per_page' => 1, 'post_status' => 'publish', 'order' => 'DESC', 'orderby' => 'date', ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { $query->the_post(); $latest_post_id = get_the_ID(); if ( $current_post_id === $latest_post_id ) { add_action( 'wp_head', 'latest_post_css' ); } else { add_action( 'wp_head', 'not_latest_post_css' ); } wp_reset_postdata(); } else { // No posts found, trigger the custom action add_action( 'wp_head', 'latest_post_css' ); } function not_latest_post_css() { // Custom function for the not latest post CSS echo '<style>#main { color: green; }</style>'; } function latest_post_css() { // Custom function for the latest post CSS echo '<style>#main { color: blue; }</style>'; }
the code above always triggers the not_latest_post_css function and shows it everywhere on the site - no matter if I'm browsing a page or a post. So the check isn't performed correctly. Could you please tell me where I'm wrong?
Thanks in advance!
May 16, 2023 at 5:35 PM #41835Peter
ModeratorHello,
Just to clarify, the issue relates to the Custom Post Type and it's not an issue of Meta Box itself. MB CPT helps you to register the post type with UI instead of using the standard WordPress code https://developer.wordpress.org/reference/functions/register_post_type/
Secondly, you can try to change the code
$current_post_id = get_the_ID();
to$current_post_id = get_queried_object_id();
to get the current post ID.May 17, 2023 at 12:07 AM #41839Val
ParticipantHi, thanks for answering! Unfortunately it didn't work, maybe there was something else wrong with the code snippet. I got another one that let me inline css with a shortcode. Here's the workaround in case anyone needs such a thing :
function get_latest_project() { $current_post_id = get_the_ID(); $args = array( 'post_type' => 'projects', 'posts_per_page' => 1, 'post_status' => 'publish', 'order' => 'DESC', 'orderby' => 'date', ); $query = new WP_Query( $args ); $latest_post_id = null; if ( $query->have_posts() ) { $query->the_post(); $latest_post_id = get_the_ID(); wp_reset_postdata(); } $css = '<style>/* not latest */</style>'; if ($latest_post_id AND $current_post_id == $latest_post_id) { $css = '<style>/* latest */</style>'; } return $css; } add_shortcode( 'latest_project', 'get_latest_project' );
-
AuthorPosts
- You must be logged in to reply to this topic.