• I’m trying to filter the title of a custom post type archive, but I seem to have gotten stuck.

    add_filter('wp_title', 'vehicle_listing_title');
    function vehicle_listing_title( $title ) 
    {
      if ( get_post_type() == 'vehicles' )
      {
        $location = get_the_terms($post->ID, 'vehicle_location');
        $model = get_the_terms($post->ID, 'vehicle_model');
        $title = $model . 'used cars for sale in' . $location .'on'. get_bloginfo('name');
      }
      return $title;
    }

    This code works only when there is content. but I want the code to also work when there is Nothing Found. We couldn't find what you're looking for

Viewing 7 replies - 1 through 7 (of 7 total)
  • What your seeing is a 404 query so you are going to have to check if the page is a vehicle listing query and a 404. Get post type won’t work since there is no posts to check the type on. The location and model stuff isn’t going to work when there are no posts though.

    Thread Starter Nkululeko

    (@oieniuz)

    Hi Csloisel,

    Well the code does work when there are posts. What do you suggest?

    It depends what you are trying to do here. Your code says put the terms of the post in the title, but there are no posts to get the terms of. Are you trying to display all terms when there is no title? What is the goal?

    Thread Starter Nkululeko

    (@oieniuz)

    Okay, what I’m trying to achieve are correct titles for filtered results. for example I Have urls like these www.example/vehicles/location/london/model/BMW right. but then the title would show as London archves - sitename. I need it to return BMW used cars for sale in London on sitename

    Basically I’m doing this to avoid duplicate titles showing on search engines, cause I have these urls hooked in a sitemap

    How would I just get the terms and not the posts?

    • This reply was modified 7 years, 1 month ago by Nkululeko.

    I recommend using get_query_var() to look up the post type, and the terms instead of relying on the post_ids. This will give you the queried values instead of the values of the first post which you are currently doing.

    Thread Starter Nkululeko

    (@oieniuz)

    Alright I’ll try that. but won’t get_query_var() return a slug and not the actual term name?

    Yes but you can always use the slug to lookup the term and get the name using get_term_by().

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to filter wp_title of a custom post type and display it on the CPT page’ is closed to new replies.