admin管理员组

文章数量:1314221

I have created a custom post type called jugadores in a wordpress site, this custom post type includes custom meta fields for storing custom fields, this include things like the number of the player, name etc, I would like to use this information y a query that will present all the players in a page section, but it is not working

            <?php 

        function inicializar_post_jugadores()
        {
            $etiquetas = array(
                'name' => 'Jugadores',
                'singular_name' => 'Jugadores',
                'add_new' => 'Agregar Jugador o Cuerpo Técnico',
                'all_items' => 'Toda la plantilla',
                'add_new_item' => 'Agregar Jugador o cuerpo Técnico',
                'edit_item' => 'Editar Jugador o Cuerpo Técnico',
                'new_item' => 'Nuevo Jugador o Cuerpo Técnico',
                'view_item' => 'Ver Jugador o Cuerpo Técnico',
                'search_item'=> 'Buscar Jugador o Cuerpo Técnico',
                'not_found' => 'No se encontro el perfil',
                'not_found_in_trash' => 'No hay perfiles en la papelera',
                'parent_item_colon' => 'Lista Padre'
            );

            $argumentos = array(
                'labels' => $etiquetas,
                'public' => true,
                'has_archive' => false, 
                'publicly_queryable' => true,
                'query_var' => true,
                'rewrite' => true,
                'capabality_type' => 'post',
                'hierarchical' => false,
                'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
                'menu_position' => 5,
            
                'exclude_from_search' => false      
            );

            register_post_type( 'jugadores', $argumentos);
        }

        add_action( 'init', 'inicializar_post_jugadores');



        function agregar_tipo_post_jugadores_a_categorias($query) 
        {
        if( is_category() ) 
        {
            $post_type = get_query_var('post_type');
            
            if($post_type)
            {
                $post_type = $post_type;
            }
            else
            {     
                $post_type = array('nav_menu_item', 'post', 'jugadores'); 
            }
            $query->set('post_type',$post_type);
            return $query;
        }
        }


        add_filter('pre_get_posts', 'agregar_tipo_post_jugadores_a_categorias');



        function agregar_campos_personalizados()
        {
            add_meta_box
            ( 
                'mis_campos', 
                'Campos', 
                'drwCampos_personalizados_jugadores', 
                'jugadores', 
                'normal', 
                'high' 
            );
        }

        add_action( 'add_meta_boxes', 'agregar_campos_personalizados');


        function drwCampos_personalizados_jugadores()
        {
            global $post;
            $meta = get_post_meta( $post->ID, 'mis_campos', true ); 
            
            ?>

                <input type="hidden" name="campos_post_jugadores" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">

            <section id="Datos-personales">
                <div class="text-left">
                    <h1 class="font-weight-bolder">Datos Personales</h1>
                </div>
                <div class="row">
                    <div class="col-sm-6 py-2">
                        <label for="">Nombres</label>
                        <input type="text" name="mis_campos[nombre_jugador]" id="mis_campos[nombre_jugador]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['nombre_jugador'])){ echo $meta['nombre_jugador']; } ?>">
                    </div>
                    <div class="col-sm-6 py-2">
                        <label for="mis_campos[apellido_jugador]">Apellidos</label>
                        <input id="mis_campos[apellido_jugador]" type="text" name="mis_campos[apellido_jugador]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['apellido_jugador'])){ echo $meta['apellido_jugador']; } ?>"> 
                    </div>
                    <div class="col-sm-4 py-2"> 
                        <label for="mis_campos[fecha_nacimiento]">Fecha de Nacimiento</label>
                        <input id="mis_campos[fecha_nacimiento]" type="date" name="mis_campos[fecha_nacimiento]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['fecha_nacimiento'])){ echo $meta['fecha_nacimiento']; } ?>">
                    </div>
                    <div class="col-sm-4 py-2"> 
                        <label for="mis_campos[fecha_nacimiento]">Peso Actual Libras</label>
                        <input id="mis_campos[peso_jugador]" type="number" name="mis_campos[peso_jugador]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['peso_jugador'])){ echo $meta['peso_jugador']; } ?>">
                    </div>
                    <div class="col-sm-4 py-2"> 
                        <label for="mis_campos[altura_nacimiento]">Estatura Metros</label>
                        <input id="mis_campos[altura_jugador]" type="number" name="mis_campos[altura_jugador]" class="w-100 form-control" min="0" max="4" value="<?php if (is_array($meta) && isset($meta['altura_jugador'])){ echo $meta['altura_jugador']; } ?>">
                    </div>
                    <div class="col-sm-12 py-2">
                        <label for="mis_campos[lugar_origin_jugador]">Lugar de Nacimiento</label>
                        <input id="mis_campos[lugar_origin_jugador]" type="text" name="mis_campos[lugar_origin_jugador]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['lugar_origin_jugador'])){ echo $meta['lugar_origin_jugador']; } ?>">
                    </div>
                    <div class="col-sm-12">
                    <label for="mis_campos[frase_jugador]">Frase o comentario</label>
                        <input id="mis_campos[frase_jugador]" type="text" name="mis_campos[frase_jugador]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['frase_jugador'])){ echo $meta['frase_jugador']; } ?>">
                    </div>
                </div>
                <hr>
                <div class="text-left">
                    <h1 class="font-weight-bolder">Redes Sociales</h1>
                    <div class="row">
                        <div class="col-sm-3">
                            <label class="text-center" for="mis_campos[facebook_jugador]">Página de Facebook</label>
                            <div class="text-center">
                                <i class="fab fa-facebook-square fa-2x" style="color: #395185; margin-top:"></i>
                            </div>
                            <input id="mis_campos[facebook_jugador]" type="text" name="mis_campos[facebook_jugador]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['facebook_jugador'])){ echo $meta['facebook_jugador']; } ?>" >
                        </div>
                        <div class="col-sm-3">
                            <label class="text-center" for="mis_campos[twitter_jugador]">Página de Twitter</label>
                            <div class="text-center">
                                <i class="fab fa-twitter fa-2x" style="color: #08bbee; margin-top:"></i>
                            </div>
                            <input id="mis_campos[twitter_jugador]" type="text" name="mis_campos[twitter_jugador]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['twitter_jugador'])){ echo $meta['twitter_jugador']; } ?>" >
                        </div>
                        <div class="col-sm-3">
                            <label  class="text-center" for="mis_campos[instagram_jugador]">Página de Instagram</label>
                            <div class="text-center">
                                <i class="fab fa-instagram fa-2x" style="color: #5f46b9; margin-top:"></i>
                            </div>
                            <input id="mis_campos[instagram_jugador]" type="text" name="mis_campos[instagram_jugador]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['instagram_jugador'])){ echo $meta['instagram_jugador']; } ?>" >
                        </div>
                        <div class="col-sm-3">
                            <label class="text-center" for="mis_campos[youtube_jugador]">Canal de Youtube</label>
                            <div class="text-center">
                                <i class="fab fa-youtube fa-2x" style="color: #ff0000; margin-top:"></i>
                            </div>
                            <input id="mis_campos[youtube_jugador]" type="text" name="mis_campos[youtube_jugador]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['youtube_jugador'])){ echo $meta['youtube_jugador']; } ?>" >
                        </div>
                    </div>
                </div>
            </section>
            <hr>
            <section id="Datos-jugador" class="mt-3">
                <h1 class="text-left font-weight-bolder">Datos Como Jugador</h1>
                <div class="row">
                    <div class="col-sm-4 py-2">
                        <label for="mis_campos[numero_jugador]">Número como Jugador</label>
                        <input id="mis_campos[numero_jugador]" type="number" name="mis_campos[numero_jugador]" class="form-control w-100" min="00" max="99" value="<?php if (is_array($meta) && isset($meta['numero_jugador'])){ echo $meta['numero_jugador']; } ?>">
                    </div>
                    <div class="col-sm-4 py-2">
                        <label for="mis_campos[fecha_debut]">Fecha de Debut</label>
                        <input id="mis_campos[fecha_debut]" type="date" name="mis_campos[fecha_debut]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['fecha_debut'])){ echo $meta['fecha_debut']; } ?>">
                    </div>
                    <div class="col-sm-4 py-2">
                        <label for="mis_campos[partidos_jugados]">Partidos Jugados</label>
                        <input id="mis_campos[partidos_jugados]" type="number" name="mis_campos[partidos_jugados]" class="form-control w-100" min="0" value="<?php if (is_array($meta) && isset($meta['partidos_jugados'])){ echo $meta['partidos_jugados']; } ?>">
                    </div>
                    <div class="col-sm-4">
                        <label for="mis_campos[jugador_titular]">Jugador Titular</label>
                        <input id="mis_campos[jugador_titular]" type="checkbox" name="mis_campos[jugador_titular]" class="d-block mx-auto form-control" <?php if (is_array($meta) && isset($meta['jugador_titular'])){ echo 'checked'; } ?>>
                    </div>
                    <div class="col-sm-4">
                        <label for="cars">Posición Jugador</label>
                        <select name="mis_campos[posicion_jugador]" id="mis_campos[posicion_jugador]" class="form-control w-100" value="">
                        <option value="portero" <?php if (is_array($meta) && isset($meta['posicion_jugador'])){if(strcmp($meta['posicion_jugador'], 'portero') == 0) {echo 'selected';}} ?>>Portero</option>
                        <option value="defensa" <?php if (is_array($meta) && isset($meta['posicion_jugador'])){if(strcmp($meta['posicion_jugador'], 'defensa') == 0) {echo 'selected';}} ?>>Defensa</option>
                        <option value="medio_campista" <?php if (is_array($meta) && isset($meta['posicion_jugador'])){if(strcmp($meta['posicion_jugador'], 'medio_campista') == 0) {echo 'selected';}} ?>>Medio Campista</option>
                        <option value="delantero" <?php if (is_array($meta) && isset($meta['posicion_jugador'])){if(strcmp($meta['posicion_jugador'], 'delantero') == 0) {echo 'selected';}} ?>>Delantero</option>
                        <option value="cuerpo_tecnico" <?php if (is_array($meta) && isset($meta['posicion_jugador'])){if(strcmp($meta['posicion_jugador'],'cuerpo_tecnico') == 0) {echo 'selected';}} ?>>Cuerpo Técnico</option>
                        </select>
                    </div>
                    <div class="col-sm-4">
                        <label for="mis_campos[posicion_especializada]">Posicion especializada</label>
                        <input id="mis_campos[posicion_especializada]" type="text" name="mis_campos[posicion_especializada]" class="w-100 form-control" value="<?php if (is_array($meta) && isset($meta['posicion_especializada'])){ echo $meta['posicion_especializada']; } ?>">
                    </div>
                </div>
            </section>  
            

                <?php
        }

        function guardar_campos_jugadores_personalizados( $post_id ) 
        {
            // verify nonce
            if (isset( $_POST['campos_post_jugadores'])) 
            {
                if ( !wp_verify_nonce( $_POST['campos_post_jugadores'], basename(__FILE__) ) ) {
                    return $post_id;
                }
            }

            // check autosave
            if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
                return $post_id;
            }
            // check permissions
            if ( 'page' === $_POST['post_type'] ) {
                if ( !current_user_can( 'edit_page', $post_id ) ) {
                    return $post_id;
                } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
                    return $post_id;
                }
            }

            $old = get_post_meta( $post_id, 'mis_campos', true );
            $new = $_POST['mis_campos'];

            if ( $new && $new !== $old ) {
                update_post_meta( $post_id, 'mis_campos', $new );
            } elseif ( '' === $new && $old ) {
                delete_post_meta( $post_id, 'mis_campos', $old );
            }
        }
        add_action( 'save_post', 'guardar_campos_jugadores_personalizados' );

        ?>

and this is the query I am trying to use, to fetch the player whose position in the field is striker or "delantero"

                    <?php 

                        $argumentos_consulta = array( 'post_type' => 'jugadores', 'orderby' => 'date', 'order' => 'ASC', 'nopaging'=> 'true', 'meta_query' => array(
                            array(
                                'meta_key' => 'posicion_jugador',
                                'meta_value' => 'delantero',
                            ) ));
                        $consulta_jugadores = new WP_Query($argumentos_consulta);
                     
                        if ($consulta_jugadores->have_posts()) 
                        {
                            while ($consulta_jugadores->have_posts()) 
                            {
                                
                                $consulta_jugadores->the_post();
                                $meta = get_post_meta( $post->ID, 'mis_campos', true);

                                get_template_part( 'template-parts/partes/carta_jugador');
                                                                           
                            }
                        }
          
                ?>

and this is another form of the query

$argumentos_consulta = array( 'post_type' => 'jugadores','nopaging'=> 'true', 'meta_key' => 'posicion_jugador', 'meta_value' => 'delantero' );

but all of them result in returning all the custom post without taking into consideration de data on the custom field.

I consulted question like this How to order custom post type by multiple custom fields? where the meta fields are suggested to query the custom fields but in my case those has not worked, so I assume and mistake in the creation of the custom post initialization is happening, but so far I have been able to pin point it,

thank you very much in advance!!

本文标签: wp queryProblem querying Custom post type by custom fields