• Resolved reypm

    (@reypm)


    I have created this Metabox:

    add_filter( 'rwmb_meta_boxes', 'cpt_opinion_register_meta_boxes' );
    
    function cpt_opinion_register_meta_boxes( $meta_boxes )
    {
        $prefix = 'cpt_opinion_';
    
        $meta_boxes[] = array(
            'id'       => 'show_at_home',
            'title'    => __( 'Opciones' ),
            'pages'    => array( 'opinion' ),
            'context'  => 'normal',
            'priority' => 'high',
            'fields'   => array(
                array(
                    'name'    => __('Mostrar opinión en portada'),
                    'id'      => $prefix.'show_at_home',
                    'type'    => 'checkbox'
                ),
            )
        );
    
        return $meta_boxes;
    }

    Now I need to find each opinion

    $normal_args   = array(
        'ignore_sticky_posts' => 1,
        'order'               => 'desc',
        'meta_query'          => array(
            array(
                'key'     => '_custom_blog_enhome',
                'value'   => '1',
                'compare' => '='
            )
        ),
        'post_status'         => 'publish',
        'posts_per_page'      => 6,
        'post_type'           => array('post','opinion','especiales','clasificados','portadadeldia','anunciantes')
    );
    
    $normal_query  = new WP_Query( $normal_args );

    How do I get the value set by the metabox in order to pass to the $normal_args parameters?

    https://www.ads-software.com/plugins/meta-box/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Anh Tran

    (@rilwis)

    I see you’re trying to get posts which has “cpt_opinion_show_at_home” checked, aren’t you?

    For checkbox field type, value is saved as 1 or 0 in the custom field, so you just need to add the following code to your meta_query:

    'meta_query'          => array(
            array(
                'key'     => '_custom_blog_enhome',
                'value'   => '1',
                'compare' => '='
            ),
            array(
                'key'     => 'cpt_opinion_show_at_home',
                'value'   => 1,
            ),
        ),
    Thread Starter reypm

    (@reypm)

    Yes, this is what I need but if I add:

    array(
        'key'     => 'cpt_opinion_show_at_home',
        'value'   => 1,
    )

    The WP_Query returns 0 rows, why is that?

    Plugin Author Anh Tran

    (@rilwis)

    You have 2 rules in meta query:
    _custom_blog_enhome=1 and
    cpt_opinion_show_at_home=1

    Posts must satisfy all these conditions to be shown. Can you check that?

    Here’s the documentation from the Codex for WP_Query and meta query.

    Thread Starter reypm

    (@reypm)

    Thanks I read the docs and add the OR at meta_query and things works:

    'meta_query' => array(
       'relation' => 'OR',
       array(
           'key'     => '_custom_blog_enhome',
           'value'   => '1',
           'compare' => '='
       ),
       array(
          'key'     => 'cpt_opinion_show_at_home',
          'value'   => 1,
       )
    )
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get the value set by the metabox’ is closed to new replies.