Add Conditional to WordPress Custom Post Plugin?

Hi all.
I’m familiar with coding but I don’t know php nor much WordPress.
I have a custom plugin which counts the # of posts in a blog and injects the Post Title and Post # into the top of the post before displaying it. I want to add a conditional test, causing it to only add the Title and Post # to posts that have a Post # below a defined threshold.

Current File: single-questions.php:

<?php
/**
 * The custom questions post type template
 */
//Add featured image above post
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_close', 15 );
remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 );
add_action( 'genesis_before_entry', 'minimum_questions_grid' );
function minimum_questions_grid() {
  echo '<div class="questions-image">'. get_the_post_thumbnail( $id, array(110,110) ).'</div>';
}
  
add_filter( 'genesis_post_info', 'ck_new_post_info' );
function ck_new_post_info( $post_info ) {
  global $post;
  $post_id = $post->ID;
  $args = array (
    'post_type'              => array( 'questions' ),
    'order'                  => 'ASC',
    'orderby'                => 'date',
    'posts_per_page'  => '-1',
  );
  $query = new WP_Query( $args );
  if ( $query->have_posts() ) {
    $counter = 0;
    while ( $query->have_posts() ) {
      $counter++;
      $query->the_post();
      if ($post->ID == $post_id) $counter_text = "CQ #" . $counter . " - ";
    }
  } else {
    // no posts found
  }
  wp_reset_postdata();
  $post_info = $counter_text . "[post_date]"; 
  return $post_info;   
}

//* Remove the entry meta in the entry footer
remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_open', 5 );
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_close', 15 );

//* Run the Genesis loop
genesis();

The modification in pseudo code:

$threshold = 107;
If (CQW# < $threshold) {
inject Title and Post # (what we are currently doing)
} else {
don’t inject Title and Post #
}

Thanks in advance for assistance.

The function that gets the post number is called ck_new_post_info and is called using the statement: add_filter( 'genesis_post_info', 'ck_new_post_info' );

Try wrapping that in the conditional:

$threshold = 107;
If (CQW# &lt; $threshold) {
   add_filter( 'genesis_post_info', 'ck_new_post_info' );
} 

You don’t need the else... part of the conditional.

Thanks for the reply.
I tried this on a test site and I get:

Notice : Undefined variable: id in D:\Projects\Bitnami\apps\wordpress\htdocs\wp-content\themes\milkmob\single-questions.php on line 10

CQW# is constructed inside of ck_new_post_info(). How do adjust for that?
Thanks again.

Are you referring to $counter_text? If so, did you want to compare it to $counter? If so, test for the threshold right after the counter, and if it does not satisfy the condition, set $counter_text to an empty string. And forget about my first suggestion.

Yes, that is what I want to do. We are going to put the Title and Post# in manually, but it will take some time to go back and do the whole blog - hence the conditional…

As I mentioned, I’m not a WP dev. What I’m getting now is:

Notice : Undefined variable: id in D:\Projects\Bitnami\apps\wordpress\htdocs\wp-content\themes\milkmob\single-questions.php on line 10

And I see that indeed, $id is not defined - but that is how it is written in the live plugin which is working.

If this is a hassle, no worries, I will find a dev to make the modifications - I thought it would be simple!
Based on your suggestion, what I wrote is:

add_filter('genesis_post_info', 'ck_new_post_info');
function ck_new_post_info($post_info) {
  global $post;
  $post_id = $post -> ID;
  $threshold = 111;
  $args = array(
    'post_type'       => array('questions'),
    'order'           => 'ASC',
    'orderby'         => 'date',
    'posts_per_page'	=> '-1',
  );
  $query = new WP_Query($args);
  if ($query -> have_posts()) {
    $counter = 0;
    while ($query -> have_posts()) {
      $counter++;
      $query -> the_post();
      if ($post -> ID == $post_id) {
        if ($counter < $threshold) {
          $counter_text = "";
        } else {
          $counter_text = "CQ #".$counter. " - ";
        }
      }
    }
  } else {
    // no posts found
  }
  wp_reset_postdata();
  $post_info = $counter_text. "[post_date]";
  return $post_info;
}

Switch this around. You have the $counter_text as empty if the threshold is below a certain value. You want it to be empty in the ‘else’ part of the conditional.

I don’t see any variable id in that code.

By the way, I would write this without spaces

ie $post->ID

Progress! The Post # code is working!

$id is in the original post at the top of this page - I didn’t want to add unecessary clutter to this page by reposting…

Is it possible to also remove the Title from the posts in which we’ve removed the Post#?
There is a separate plugin which builds the posts:
questions-plugin.php:

<?php
/*
Plugin Name: Question of the Week
Description: CPT for Question of the Week
*/
/* Start Adding Functions Below this Line */

// Our custom post type function
function create_cq_posttype() {

	register_post_type( 'questions',
	// CPT Options
		array(
			'labels' => array(
				'name' => __( 'Questions' ),
				'singular_name' => __( 'Question' )
			),
			'public' => true,
			'has_archive' => true,
			'rewrite' => array('slug' => 'questions'),
			'supports' => array( 'title', 'editor', 'genesis-seo', 'thumbnail','genesis-cpt-archives-settings', 'comments' ), // to add a title or add text above the posts page. 
		)
	);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_cq_posttype' );

/*
* Creating a function to create our CPT
*/

function custom_cq_post_type() {

// Set UI labels for Custom Post Type
	$labels = array(
		'name'                => _x( 'Questions', 'Post Type General Name', 'mm' ),
		'singular_name'       => _x( 'Question', 'Post Type Singular Name', 'mm' ),
		'menu_name'           => __( 'Questions', 'mm' ),
		'parent_item_colon'   => __( 'Parent Question', 'mm' ),
		'all_items'           => __( 'All Questions', 'mm' ),
		'view_item'           => __( 'View Question', 'mm' ),
		'add_new_item'        => __( 'Add New Question', 'mm' ),
		'add_new'             => __( 'Add New', 'mm' ),
		'edit_item'           => __( 'Edit Question', 'mm' ),
		'update_item'         => __( 'Update Question', 'mm' ),
		'search_items'        => __( 'Search Question', 'mm' ),
		'not_found'           => __( 'Not Found', 'mm' ),
		'not_found_in_trash'  => __( 'Not found in Trash', 'mm' ),
	);
	
// Set other options for Custom Post Type
	
	$args = array(
		'label'               => __( 'questions', 'mm' ),
		'description'         => __( 'Questions news', 'mm' ),
		'labels'              => $labels,
		// Features this CPT supports in Post Editor
		'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
		// You can associate this CPT with a taxonomy or custom taxonomy. 
		//'taxonomies'          => array( 'genres' ),
		/* A hierarchical CPT is like Pages and can have
		* Parent and child items. A non-hierarchical CPT
		* is like Posts.
		*/	
		'hierarchical'        => true,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
		'show_in_admin_bar'   => true,
		'menu_position'       => 5,
		'can_export'          => true,
		'has_archive'         => true,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
	);
	
	// Registering your Custom Post Type
	register_post_type( 'questions', $args );
}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( 'init', 'custom_cq_post_type', 0 );

?>

Somewhere in that plugin there should be a loop something like this:

$query = new WP_Query($args);
  if ($query -> have_posts()) {
      while ($query -> have_posts()) {
         $query->the_post();
        // some code
       }
     }

In that loop, there should be the_title(); and the_content();. Would you be able to find the_title(); and include a conditional there also?

By the way, you should never change the code of the theme, because if it is updated (and never ignore those updates for security reasons) you will lose all your changes. You really should be working with a child theme.

My bad.
I posted the wrong code in my previous post. To avoid unecessary clutter, I edited it. See questions-plugin.php above.
Both of these plugins are part of a custom Genesis child theme.

Just a followup that I resolved this problem, thanks to the help from WebMachine and a plugin by Bill Erickson.
A summary:
To remove Titles, use wordpress.org/plugins/genesis-title-toggle.
Instructions: https://github.com/billerickson/Genesis-Title-Toggle/wiki/Customization

To enable Bill’s plugin for ‘questions’ posts (add an option to hide title on the page in the backend), I added this to functions.php:

/**

 * Add Genesis Title Toggle to Posts

 *
 * @see https://www.billerickson.net/code/genesis-title-toggle-for-posts
 * @author Bill Erickson
 *
 * @param array $post_types
 * @return array
 */

function be_title_toggle_on_posts( $post_types ) {
$post_types[] = 'questions';
return $post_types;
}

add_filter( 'be_title_toggle_post_types', 'be_title_toggle_on_posts' );

To remove the Post# and date, add 2 conditionals and $threshold to single-questions.php :

add_filter('genesis_post_info', 'ck_new_post_info');
function ck_new_post_info($post_info) {
global $post;
$post_id = $post -&gt; ID;
$threshold = 127;
$args = array(
'post_type' =&gt; array('questions'),
'order' =&gt; 'ASC',
'orderby' =&gt; 'date',
'posts_per_page' =&gt; '-1',
);

$query = new WP_Query($args);
if ($query -&gt; have_posts()) {
$counter = 0;
while ($query -&gt; have_posts()) {
$counter++;
$query-&gt;the_post();
if ($post-&gt;ID == $post_id) {
// echo 'made it here. Counter: ', $counter,&quot; Threshold: &quot;,$threshold;
if ($counter &lt; $threshold) {
$counter_text = &quot;CQ #&quot;.$counter. &quot; - &quot;;
} else {
$counter_text = &quot;&quot;;
}
}
}
} else {
// no posts found
}
wp_reset_postdata();
if(!empty($counter_text)){
$post_info = $counter_text. &quot;[post_date]&quot;;
return $post_info;
}
}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.