Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a seriously problem :( maybe someone can help me

The users registered in my wordpress page have one post only, and i created custom_post_type named "ofertas". The users can create one oferta only. With this code show the oferta title on index.php, the problem is show the oferta title in ALL POSTS. I want only show his own oferta title (if they have).

<?php 
// First loop
while ( have_posts() ): the_post(); 
author_id = get_query_var('author');
    $ofertas = array();

// Second Loop
$i = 0;
$args = array(
    'author' => $author_id,
    'post_type' => 'ofertas'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) : 
    while ($the_query->have_posts()) : $the_query->the_post(); // check if it has offers, if it has, loop over the offers
        $ofertas[$i]['title'] = get_the_title(); // or anything you need
        $i++;
    endwhile; // close the loop
else: // if it has no post
    continue; // we don't display the post
endif;

wp_reset_postdata(); 
?>
<div class="post">
<?php
the_title(); // or anything you need

foreach ($ofertas as $oferta):
    echo $oferta['title']; // we display the title of the offer
endforeach;
?>
</div>
<?php  endwhile; ?>

I created the custom_post_type with this code

add_action('init', 'crear_tipo_ofertas', 0);

function crear_tipo_ofertas() {
$labels = array(
  'name'               => _x( 'Ofertas', 'Post Type General Name', 'text_domain' ),
  'singular_name'      => _x( 'Oferta', 'Post Type Singular Name', 'text_domain' ),
  'menu_name'          => __( 'Ofertas', 'text_domain' ),
  'parent_item_colon'  => __( 'Oferta Padre', 'text_domain' ),
  'all_items'          => __( 'Ofertas', 'text_domain' ),
  'view_item'          => __( 'Ver Oferta', 'text_domain' ),
  'add_new_item'       => __( 'Añadir Oferta Nueva', 'text_domain' ),
  'add_new'            => __( 'Añadir', 'text_domain' ),
  'edit_item'          => __( 'Editar Oferta', 'text_domain' ),
  'update_item'        => __( 'Actualizar', 'text_domain' ),
  'search_items'       => __( 'Buscar Ofertas', 'text_domain' ),
  'not_found'          => __( 'Ofertas no encontradas', 'text_domain' ),
  'not_found_in_trash' => __( 'Ofertas no encontradas en Papelera', 'text_domain' ),
  );

$rewrite = array(
  'slug'                => 'oferta',
  'with_front'          => true,
  'pages'               => true,
  'feeds'               => true,
  );

$args = array(
  'label'               => __( 'oferta', 'text_domain' ),
  'description'         => __( 'Ofertas Spas y Balnearios', 'text_domain' ),
  'labels'              => $labels,
  'supports'            => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields'),
  'taxonomies'          => array( 'category', 'post_tag' ),
  'hierarchical'        => false,
  'public'              => true,
  'show_ui'             => true,
  'show_in_menu'        => true,
  'show_in_nav_menus'   => true,
  'show_in_admin_bar'   => true,
  'menu_position'       => 5,
  'menu_icon'           => site_url().'/wp-content/plugins/my_plugin/images/logo.png',
  'can_export'          => true,
  'has_archive'         => 'ofertas',
  'exclude_from_search' => false,
  'query_var'           => 'ofertas',
  'rewrite'             => $rewrite,
  'capability_type'     => 'post',
  );

register_post_type('ofertas', $args);
}

add_action('init', 'crear_tipo_ofertas', 0);
share|improve this question
    
You only want to show the author's post? One per user? –  Strap1 Jan 9 at 22:12
    
i want show oferta per user/post @Strap1 –  Vektor Unbreakable Jan 9 at 22:55
    
and yes, the users only have permission for one post and one custom_post @Strap1 –  Vektor Unbreakable Jan 9 at 23:08

1 Answer 1

If I understand you right, you want to show a post by a certain user with a custom post type

Here is the code I would use, it might use a little more customizing, but it should give you the effect you want.

<?php
function display_oftera_post () {
    global $current_user;
    get_currentuserinfo(); // Logged in User

    if ( is_user_logged_in() ) { // If User is logged in, display the post

        $args = array( // Post Arguments
            'post_type' => 'oftera', // Post Type
            'author'    => $current_user->ID
        );

        $the_query = new WP_Query( $args);
                while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
                                     <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                                     <p> <?php the_content(); ?> </p>
        <?php endwhile;
        }
    }
?>
share|improve this answer
    
this code paste inside index.php? –  Vektor Unbreakable Jan 10 at 13:03
    
the user is not logging. I need author of post, not user login –  Vektor Unbreakable Jan 10 at 13:26
    
This would be in the function.php and call the function up on index.php using <?php display_oftera_post(); ?> but it seems I don't understand what your trying to accomplish. –  MrJustin Jan 10 at 23:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.