Support » Fixing WordPress » How do I exclude the current post from a filter hook function?

  • Hi there,

    I am adding a class to the title, except the backend:

    function add_notypo_to_title( $title ) {
     
        return "<span class='noTypo'>$title</span>";
    }
    
    if (! is_admin() ) {
        add_filter( 'the_title', 'add_notypo_to_title', 10, 1 );
    }

    I want to exclude the current post title, the title of the post that is currently being viewed.

    I have tried the following:

    function add_notypo_to_title( $title ) {
     
        return "<span class='noTypo'>$title</span>";
    }
    
    $current_id = function_exists( 'vcex_get_the_ID' ) ? vcex_get_the_ID() : get_the_ID();
    if $title !== get_the_title( $current_id ) )  {
       
     
    
        add_filter( 'the_title', 'add_notypo_to_title', 10, 1 );
    
    }

    But it results in a critical error.

    Something else that would achieve the same result is to limit adding the class to only my related posts:

    function add_notypo_to_title( $title ) {
     
        return "<span class='noTypo'>$title</span>";
    }
    
    $referer = wp_get_referer();
                $post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;
                $related = get_post_meta( $post_id, 'related_post_ids', true );
    
               
    if (! is_admin() ) {
         if (  empty( $related ) ) {
     
    
        add_filter( 'the_title', 'add_notypo_to_title', 10, 1 );

    But this also results in a critical error.

    Excluding the current post would be a more generic solution, I guess.

    What is the correct way to exclude the current post?

Viewing 7 replies - 1 through 7 (of 7 total)
  • I want to exclude the current post title, the title of the post that is currently being viewed.

    If you can explain in which section you’re trying to exclude this thing and possible share the website URL to review it then we could be able to provide a better solution for making it possible.

    Generally, the method and hook you’re using is not the correct way to do this. the_title() function is for all the post types so it works for navigation menu, post, pages etc. so it’s not to use that.

    Thread Starter berry metal

    (@erikalleman)

    Hi,

    there is no section, it’s simply the current post.

    In the current post I have 2 current post titles:
    the regular post title and the post title in the breadcrumbs.

    It’s not necessary to reveal my project, this is a generic PHP – WordPress question.
    Are there several current post conditionals in WordPress that can be defined?
    I only know about 1 current post, the one that is currently open in the browser – that is what I mean.

    The goal is to add the class to the title – only for my related posts.
    The related posts – like normally, are on the same page as the current post, in my case in a page builder column that is part of my post template, since I don’t use a sidebar.
    The related posts are assigned manually by specifying their ID in a field in the post settings – this is a feature of my theme.
    The related posts are fetched by a grid, but the variable $grid_id is not yet available at the time the global the_title hook is running, so I cannot target it- at least this is what I was told by the developer, I personally don’t have a thourough understanding of how my grid works.
    That being said, other parts of my post are also output by a grid, that has a different ID, hence I would need to target the grid ID, or to target the meta information if it exists – that refers to those posts as related posts, since they were defined as related posts by entering the post IDs in a field in the post settings.

    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    Thread Starter berry metal

    (@erikalleman)

    If my related posts were added by specifying post IDs in a custom field in the post settings, does that imply that the related posts have some post meta that could be targeted in the conditional?

    The best option to modify the related post titles is by finding and modifying the template files in your theme.

    Thread Starter berry metal

    (@erikalleman)

    Thanks,
    I just asked the grid developer about the templates, because I am not sure where they are and how do they work.

    Thread Starter berry metal

    (@erikalleman)

    This filter hook successfully outputs all my related posts that were assigned to the current post, in each post, into the grid with ID 3, which is in my post template:

    add_filter(
        'wp_grid_builder/grid/query_args',
        function( $query_args, $grid_id ) {
    
            global $post;
    
            if ( 3 === $grid_id ) {
    
                $referer = wp_get_referer();
                $post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;
                $related = get_post_meta( $post_id, 'related_post_ids', true );
    
                if ( ! empty( $related ) ) {
    
                    $query_args['post__in'] = explode( ',', $related );
                    unset( $query_args['category__in'] );
    
                }
            }
    
            return $query_args;
    
        },
        10,
        2
    );

    Based on this hook it should be possible to write a conditional to limit adding the class to only the related posts titles.

    This is what I could write, but it’s wrong (critical error):

    function add_notypo_to_title( $title, $query_args ) {
     
     global $post;
     $referer = wp_get_referer();
                $post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;
                $related = get_post_meta( $post_id, 'related_post_ids', true );
              $query_args = array(
              'post__in' => array( $post->ID )
    );
              $related_posts = get_posts($args);
    
        return "<span class='noTypo'>$title</span>";
    }
    
    if (! is_admin() ) {
     
        add_filter( 'the_title', 'add_notypo_to_title', 10, 2 );
    }

    Can someone help me out with this?
    I don’t need $grid_id, so I don’t care if it’s not available when the_title hook runs.
    I just need a correct conditional to limit adding the class to my related post titles.
    Question is, is $related available when the_title hook runs or how do I make it available?
    How do I determine that?
    What is the correct way to put this:
    'post__in' => $related
    into a conditional?

    Thanks in advance.

    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    • This reply was modified 3 months, 2 weeks ago by berry metal.
    Thread Starter berry metal

    (@erikalleman)

    And this is the code that is adding the metabox for specifying related posts for each post:

    // Add related items setting for standard posts
    add_filter( 'wpex_metabox_array', function( $array, $post ) {
    
    	if ( 'post' == $post->post_type ) {
    
    	    $array['main']['settings']['related_post_ids'] = array(
    	        'title'         => __( 'Related Posts', 'total' ),
    	        'description'   => __( 'Comma seperated ID\'s for related items', 'total' ),
    	        'id'            => 'related_post_ids', 
    	        'type'          => 'text',
    	    );
    
    	}
    
        // Return fields
        return $array;
    
    }, 40, 2 );
    
    // Alter related items if the related_post_ids meta field is set
    // NOTE: for custom post types use the "wpex_related_{$post_type}_args" filter instead of "wpex_blog_post_related_query_args".
    add_filter( 'wpex_blog_post_related_query_args', function ( $args ) {
    	
    	if ( $related = get_post_meta( get_the_ID(), 'related_post_ids', true ) ) {
    		$args[ 'category__in' ] = null;
    		$args[ 'post__in' ] = explode( ',', $related );
    	}
    
    	// Return args
    	return $args; 
    
    } );
Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.