0

This is a silly question, but I can't find the proper way to ask it to google. so, sorry if this is a repetitive question.

I give a custom field with a checked that the user can check if he want this specific post to go the home page. So in my home page, I call for all the post who has the checked activated.

I am creating my custom field for the custom post type CV:

function add_custom_post_meta_box() {
    add_meta_box(
        'custom_post_meta_box', // $id
        'Campos Personalizados', // $title
        'show_custom_post_meta_box', // $callback
        'cv', // $screen
        'normal', // $context
        'high' // $priority
    );
}
add_action( 'add_meta_boxes', 'add_custom_post_meta_box' );

But inside this custom post meta box, I have multiple fields, like a label and a checkbox:

function show_custom_post_meta_box() {
    global $post;
        $meta = get_post_meta( $post->ID, 'your_fields', true ); ?>
  <p>
    <label for="your_fields[text]">Especialidad</label>
    <br>
    <input type="text" name="your_fields[text]" id="your_fields[text]" class="regular-text" value="
    <?php if (is_array($meta) && isset($meta['text'])){ echo $meta['text'];} ?>">
  </p>
  <p>
    <label for="your_fields[checkbox]">Mostrar en Home
        <input type="checkbox" name="your_fields[checkbox]" value="checkbox"
      <?php
      if (is_array($meta) && isset($meta['checkbox'])){
        if ( $meta['checkbox'] === 'checkbox' ) echo 'checked';
      }
      ?>>
    </label>
  </p>

Okey, the rest of the code save the custom field and works perfectly. My problem is when I try to call it:

$posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'cv'
    // 'meta_key'       => 'your_fields[checkbox]', //This is wrong!!
    // 'meta_value' => 'checkbox'
));

And when I try to call the post info:

<?php
        foreach( $posts as $post ):
            setup_postdata( $post );
        ?>

              <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
              <p>
                <?php
                  $meta = get_post_meta( $post->ID, 'your_fields', true );
                  if (isset($meta['checkbox'])){
                    echo $meta['checkbox'];
                  }
                ?>
              </p>
       <?php endforeach; ?>

So, my question is: How can make my query, to bring me just the custom post types, with a checked activated.

1
  • What's the output of var_dump( $posts )?
    – middlelady
    Commented Oct 21, 2018 at 16:44

1 Answer 1

1

If you want to use post meta for setting which posts are displayd on the front page, then I would recommend saving the setting with a separate meta_key. So instead of pushing it to the your_fields save it with its own key.

This means modifying your metabox content to have something like this,

<input type="checkbox" name="show_on_front_page" value="true"

Please modifiy your metabox saving box also accordingly.

Then you should be able to use the get_posts like so,

$posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'cv',
    'meta_key'       => 'show_on_front_page',
    'meta_value' => 'true'
));

But just yesterday I saw a comment by Mr. Tom J Nowell on some other thread, that this is rather inefficient way of setting front page posts. On a larger site this could, in the worst case scenario, grind your site's server to a halt.

You would be better off using a (private) custom taxonomy to which you would assign posts you want to show on the front page. Getting posts with a certain taxonomy from the database is faster and more efficient way of doing things.

From Mr. Nowell's blog, https://tomjn.com/2018/03/16/utility-taxonomies/

Set up custom taxonomy,

function tomjn_utility_taxonomy() {
$args = array(
    'label'     => __( 'Internal Markers', 'tomjn' ),
    'public'    => false,
    'rewrite'   => false,
    'show_ui'   => true, // you'd need this if you want to show the taxonomy metabox on the post edit screen
);
register_taxonomy( 'utility', 'post', $args );
}
add_action( 'init', 'tomjn_utility_taxonomy', 0 );

Automate publishing to front page,

function tomjn_auto_add_to_home( $post_id ) {
wp_set_object_terms( $post_id, 'show_on_homepage', 'utility', true );
}
add_action( 'publish_post', 'tomjn_auto_add_to_home' );

adjust the homepage query

function tomjn_only_show_home( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'utility', 'show_on_homepage' );
    }
}
add_action( 'pre_get_posts', 'tomjn_only_show_home' );

Please refer to the blog post for more detailed explanation.

2
  • thanks for sharing this new approach. I will give it a try tomorrow. Just a question, That new taxonomy that you define can be modified by the user?. Where? Can you share a print screen of how it will be in the admin page?. thanks
    – ValRob
    Commented Oct 21, 2018 at 20:02
  • If you add the show_ui => true parameter to register_taxonomy $args, then the custom taxonomy should be displayed on the post edit screen in the same way as the default post category is thus enabling the user to add/remove terms as needed. Please refer to the register_taxonomy codex entry for more details. Commented Oct 22, 2018 at 6:19

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.