Support » Plugin: Carbon Fields » Carbon Fields and Search

  • Resolved Sean T.

    (@tinothepro)


    Hi,

    I am working on a site that utilizes CF very heavily and I am running into some issues with search. It appears that any content in these CF meta boxes is noticed by WordPress Search but when it comes to displaying results it shows empty tags with no content. Is there something special I need to be adding to my theme or do I need a custom search template?

    Here is a screenshot of my dev tools upon inspection of the search page:
    https://ibb.co/fcAVA5

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Hi @tinothepro ,

    Carbon Fields do not automatically output anything to the front-end (header/footer scripts fields being the only exceptions) so you have to use the provided collection of getter methods (e.g. carbon_get_post_meta, carbon_get_theme_option etc.) to output values in your theme.

    Sean T.

    (@tinothepro)

    Hi @atanasangelovdev,

    Thanks for replying so quickly. What you said makes complete sense. Are you aware of any example search templates using CF? I am just thinking in terms of how a normal WP search returns a title and an excerpt (pretty standard), if you have multiple CF metaboxes set up and maybe some pages have different ones, how you’d query for all these and their variances (It’d be more than just the_excerpt()). I appreciate any resources or help you can provide.

    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    There isn’t an automatic way to query what fields belong to what post type. However, you can add your own logic in your search template loop based on get_post_type().

    For example:

    
    <?php
    $post_type = get_post_type();
    if ( $post_type === 'post' ) {
    	echo carbon_get_the_post_meta( 'crb_some_post_field' );
    } else if ( $post_type === 'page' ) {
    	echo carbon_get_the_post_meta( 'crb_some_page_field' );
    	echo carbon_get_the_post_meta( 'crb_some_page_field_2' );
    }
    // etc ...
    ?>
    

    Hi @atanasangelovdev,

    I had one more additional question regarding this. I tried several variations of adding this into the loop but have been unsuccessful in retrieving any data.

    <?php while ( have_posts() ) : the_post(); ?>
        <div class="result">
          <span class="search-post-title">
           <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
          </span>
          <p><?php echo carbon_get_the_post_meta( 'content_section_column1' ); ?></p>
          <span class="search-post-excerpt"><?php the_excerpt(); ?></span>
       </div>
     
      <?php endwhile; ?>

    These fields are located in a simple builder plugin. Is there something I’m missing with this? Following your example did not output anything. I realized I’m not able to pull any of the meta information from the plugin even from functions.php. Ideas?

    • This reply was modified 1 year, 12 months ago by  Sean T..
    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Can you share your container and field definitions code as well?

    @atanasangelovdev sure thing. Let me know if you need additional information. These container/field definitions are in my plugin and I have no problem displaying their contents when using echo carbon_get_the_post_meta( ‘crb_some_post_field’ ); from inside the plugin.

    I’ll link a gist here that shows the definition, initialization, and implementation in case you need more frame of reference.

    https://gist.github.com/tinothepro/d43611ae20c307b30a4c72c1f52fd023

    I appreciate all your help!

    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    The gist you shared only contains a single reference to carbon_get_* and from what I can see it is used correctly and should return an array of section entries as long as it is used in The Loop.

    As for your earlier snippet – carbon_get_the_post_meta( 'content_section_column1' ) – you cannot access fields inside a complex directly. You must get the root complex with carbon_get_* and then access the property you are interested in using the returned array.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Carbon Fields and Search’ is closed to new replies.