0

I need a code that causes: <?php while ( have_posts() ) : the_post(); ?> List according to the amount of VIEW, is like to list popular posts only that it will enter the table mh_postmeta, and list according to who has the most view.

inserir a descrição da imagem aqui

The staff uses the 'orderby' => 'comment_count', to list according to the comments, but need it to search for another table, BUT, this can only happen to certain category .. Let's say you have 40 CAT it should only list by VIEW instead of DATE in CAT 3

1
  • Have a look at this tutorial on adding Custom Meta Boxes. It's a little old, but I don't really think much has changed; smashingmagazine.com/2011/10/…
    – phatskat
    Commented Dec 14, 2015 at 15:59

4 Answers 4

1

Seems like you already have the custom field for views set up since that's a phpMyAdmin screenshot. If so, you can use order_by="post_views_count" in most wp_query based calls. For instance:

$args = array(
    'post_type' => 'your_post_type',
    'orderby'   => 'meta_value_num',
    'meta_key'  => 'post_views_count',
);
$query = new WP_Query( $args );

//...and on with the loop
<?php while ( have_posts() ) : the_post(); ?>...

Limiting this behavior to category 3 is pretty easy but how go about it depends on the context and your template setup.

More on orderby in the WP Codex

0

If you don't want to register your own meta-boxes: give advanced custom fields a go.

You can easily add meta-boxes in the sidebar (or the default: below the main editor screen)

0

As usual with WordPress there is a short answer and a complete answer. :)

In a nutshell if you want something that functions exactly like categories — you can create a new taxonomy. You can customize a lot about it, but WP also takes care about a lot of it, including providing you with ready–made meta box.

However! Not all kinds of data are suitable as taxonomy. Some things make more sense as post meta... Which has no easy/native way to provide UI for you. That is realm of building completely custom meta box (or using third party plugin/framework for such).

Learning how to get data architecture right is one of the core skills in WordPress development. It is hard to guess how deep rabbit hole can go in your case. I would say start with taxonomies and see how it works out from there.

0

If you want a meta box, this will do it. It'll be up to you what you want to put in it and how each field is saved / rendered.

This example was adapted from add_meta_box.

if ( is_admin() ) {
    add_action( 'load-post.php', 'BoxPost::init' );
    add_action( 'load-post-new.php', 'BoxPost::init' );
}

if( ! class_exists('BoxPost')) :
/**
 * The Class.
 */
class BoxPost {

  const SLUG = "boxpost_meta_box_name";
  const NONCE_SLUG = "boxpost_inner_custom_box";
  const NONCE = "boxpost_inner_custom_box_nonce";
  const TEXT_DOMAIN = 'text_domain';

    /**
     * Hook into the appropriate actions when the class is constructed.
     */
    static public function init() {
        add_action( 'add_meta_boxes', 'BoxPost::add_meta_box' );
        add_action( 'save_post', 'BoxPost::save' );
    }

    /**
     * Adds the meta box container.
     */
    static public function add_meta_box( $post_type ) {
        $post_types = array('post', 'page');   //limit meta box to certain post types
        if ( in_array( $post_type, $post_types )) {
            add_meta_box(
                  self::SLUG
                , __( 'BoxPost Headline', self::TEXT_DOMAIN )
                , 'BoxPost::render_meta_box_content'
                , $post_type
                , 'advanced'
                , 'high' // 'high', 'core', 'default' or 'low'
            );
        }
    }

    /**
     * Save the meta when the post is saved.
     *
     * @param int $post_id The ID of the post being saved.
     */
    static public function save( $post_id ) {

        /*
         * We need to verify this came from the our screen and with proper authorization,
         * because save_post can be triggered at other times.
         */

        // Check if our nonce is set.
        if ( ! isset( $_POST[ self::NONCE ] ) )
            return $post_id;

        $nonce = $_POST[ self::NONCE  ];

        // Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce, self::NONCE_SLUG ) )
            return $post_id;

        // If this is an autosave, our form has not been submitted,
        // so we don't want to do anything.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $post_id;

        // Check the user's permissions.
        if ( 'page' == $_POST['post_type'] ) {

            if ( ! current_user_can( 'edit_page', $post_id ) )
                return $post_id;

        } else {

            if ( ! current_user_can( 'edit_post', $post_id ) )
                return $post_id;
        }

        /* OK, its safe for us to save the data now. */

        // Sanitize the user input.
        $mydata = sanitize_text_field( $_POST['boxpost_meta_field'] );

        // Update the meta field.
        update_post_meta( $post_id, '_boxpost_meta_field_value_key', $mydata );
    }


    /**
     * Render Meta Box content.
     *
     * @param WP_Post $post The post object.
     */
    static public function render_meta_box_content( $post ) {

        // Add an nonce field so we can check for it later.
        wp_nonce_field( self::NONCE_SLUG, self::NONCE  );

        // Use get_post_meta to retrieve an existing value from the database.
        $value = get_post_meta( $post->ID, '_boxpost_meta_field_value_key', true );

        // Display the form, using the current value.
        echo '<label for="boxpost_meta_field">';
        _e( 'Description for this field', self::TEXT_DOMAIN );
        echo '</label> ';
        echo '<input type="text" id="boxpost_meta_field" name="boxpost_meta_field"';
        echo ' value="' . esc_attr( $value ) . '" size="25" />';
    }
}

endif; //BoxPost

To get the value later:

$value = get_post_meta( $post->ID, '_boxpost_meta_field_value_key', true );

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.