0

I created a CTP and: When display a post ID inside the loop (example "112"), I also want to add a prefix like "CFX" and display result as string "CFX+IDnumber" (example: CFX112) and use it for searching as something like bookcode, Book ID.

So, how can I do that and let the search accept that string?

1 Answer 1

0

Why don't you add a custom field to the post, so that when you save the post, you automatically create a custom field with that value, and then it's easy to search for it and also show on the page.

Save Bookcode as Post Meta

To save that custom field, you can add on your functions.php:

function save_bookcode( $post_id ) {
    // If this is just a revision, no need to save bookcode
    if ( wp_is_post_revision( $post_id ) )
        return;

    // save a custom field 'bookcode' with CFX+ID
    add_post_meta( $post_id, 'bookcode', 'CFX' . $post_id, true );  // true on last param so that this field is unique and saved only the first time
}
add_action( 'save_post', 'save_bookcode' );

In this example, the bookcode is save for any post type, but you can limit to whatever post type you use.

Show Bookcode on template

Then to show on your template, inside the loop, you can just write:

Book ID: <?php echo get_post_meta( get_the_ID(), 'bookcode', true ); ?>

Include Bookcode on search

And to include that custom field on the normal search you can use the pre_get_posts filter:

function add_bookcode_to_search( $query ) {
    if ($query->is_search && !is_admin() ) {
        $query->set( 'meta_query', array(
            array(
                'key' => 'bookcode',
                'value' => $query->query_vars['s'],
                'compare' => 'LIKE'
            )
        ));
    }

    return $query;
}
add_filter( 'pre_get_posts', 'add_bookcode_to_search' );

But this will only return results if you have the bookcode on both title/content and that meta. If you want instead to add this custom field as one more place to search for something you need to manually change the SQL query: https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/

Or you can also use a plugin that already has that work done like Search Everything or Relenvassi.

2
  • ok, sorry, probably the problem is that we're adding this to the regular search, so what it's happening is that everything you write on search input will need to show up on one of the normal fields (title, content, ...) and also on this bookcode. So, if you search for CFX123, only posts that have that on bookcode meta and on title/content will show up. To have bookcode work as a search on title and content, I think you need to create manually the query or use a plugin that already has the work done. I'll update my answer with that information.
    – dbeja
    Commented Jul 3, 2017 at 9:54
  • I've been manually modify the SQL querry and It worked perfect!!! Thank you for the big hand!!!
    – Democloak
    Commented Jul 3, 2017 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.