Forum

Thread tagged as: Question, Problem, Blog

Made custom post slug but meta is not working now

Hello,

About a week ago I opened a thread about creating a post slug by using a custom field. That is working perfectly, but now my meta_head is not outputting anything.

My top.php:

    <?php
        if (perch_layout_has('blog-post')) {
            perch_blog_post_meta(perch_get('s'));
        } else if (perch_layout_has('blog-index')){
            echo '<title>' . 'Blog' . '</title>';
        } else if (perch_layout_has('blog-archief')){
            echo '<title>' . 'Archief' . '</title>';
        } else{
            echo '<title>' . perch_pages_title(true) . '</title>';
        }
    ?>

When using perch_get('s') it outputs nothing.

I use /blog/<perch:blog id="customUrlSlug" type="hidden" /> as a path to my posts, which is working fine.

When using perch_get('customUrlSlug') it outputs meta tags from 10! posts.

I get my post on post.php like this:

    <?php
    # get the post
    $post = perch_blog_custom(array(
          'filter'        => 'customUrlSlug',
          'match'         => 'eq',
          'value'         => perch_get('s'),
          'skip-template' => 'true',
          'return-html'   => 'true',
      ));
    ?>

I tried using:

        if (perch_layout_has('blog-post')) {
            perch_blog_post_meta(array(
          'filter'        => 'customUrlSlug',
          'match'         => 'eq',
          'value'         => perch_get('s'),
          'skip-template' => 'true',
          'return-html'   => 'true',
      ));

But that is also outputting meta tags for 10 posts.

How could I get the blog post meta for a single post?

Mike Hendriks

Mike Hendriks 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Those functions are filtering on the built in postSlug. If you're not using postSlug then they won't work.

A workaround would be to find the post with your custom slug, get the default postSlug from that, and then pass that into the meta functions.

Is that not what I am trying here:

if (perch_layout_has('blog-post')) {
perch_blog_post_meta(array(
# Find the post using custom slug
'filter' => 'customUrlSlug',
'match' => 'eq',
# get the default postSlug
'value' => perch_get('s'),
'skip-template' => 'true',
'return-html' => 'true',
));

?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Mike,

I don't think you can filter using perch_blog_post_meta(): https://docs.grabaperch.com/functions/blog/perch-blog-post-meta/

perch_blog_post_meta($default_slug, $options_array, $boolean);

$default_slug has to be postSlug.

$post = perch_blog_custom(array(
'filter' => 'customUrlSlug',
'match' => 'eq',
'value' => perch_get('s'),
'skip-template' => 'true',
));

# get the default postSlug from $post
$postSlug = $post[0]['postSlug'];

perch_blog_post_meta($postSlug);

The biggest advantage of perch_blog_post_meta(), besides convince, is caching, but otherwise you can do the same thing with perch_blog_custom().

So you can use:

perch_blog_custom(array(
'filter' => 'customUrlSlug',
'match' => 'eq',
'value' => perch_get('s'),
'template' => 'your_meta_template.html',
));
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

When using perch_get('customUrlSlug') it outputs meta tags from 10! posts.

It may be wise to check what perch_get() does:

Thanks a lot Hussein,

perch_blog_custom(array(
'filter' => 'customUrlSlug',
'match' => 'eq',
'value' => perch_get('s'),
'template' => 'your_meta_template.html',
));

Works like a charm. I didn't know I could use blog_custom for that purpose, but it is pretty logical. I will definitely check out your blog post.

Mike