News just for certain group on wordpress

Asked

Viewed 254 times

6

When inserting a post in wordpress, I have the taxonomy 'group' with terms 'pharmacies, grocery stores, snack bars...', which are the groups, because I already have customers for these groups. What I need is that when logging in to have restricted access in the client area, wordpress check if you have news for the group whose client is part and, if there is, show it along with the posts specifically pro client.

To show news directed to the client I already have, I’m just not able to do this group check.

/*
Eu tenho o ID do CLIENTE na $_GET.
Tenho que buscar os grupos que o cliente participa
Checar se algum desses grupos tem vinculo com a noticia
*/
$meta_query[] = array(
    'key' => 'clientes',
    'value' => $_GET['id'],
    'compare' => '='
);

$tax_query[] = array(
    'taxonomy' => 'grupo'
);

$args = array(
    array(
        'post_type' => 'noticias'
        ,'meta_query' => $meta_query
    )
);    

$tmp = new WP_Query($args);

if(!$tmp->have_posts()){
    echo 'não tem notícias no momento';
    return;
}

while ($tmp->have_posts()){
    $tmp->the_post(); 
    echo get_the_title().'<br>';
}

Note: The client is inserted in the 'clients' post_type. In this post_type I have the email fields, password and the groups are listed in the form of taxonomy to be able to link the client to the group.

  • The login is not done in the WP pattern. It has a specific form for it. Where from this form is picked up the 'id' of the client to do the news access checks.

  • Flávia Amaral, you are saving the user group in the correct postmeta?

  • Yes, yes Leandro

2 answers

1


It wouldn’t be better for you to change the key to cliente_group and its value would be the group belonging to the client, as you are already passing the client id in the url, it would get like this:

$group_client = get_post_meta( $_GET['id'], 'cliente_group', true );

                $type = 'noticias';
            $args=array(
                'post_type' => $type,
                'post_status' => 'publish',
                'posts_per_page' => -1,
                                    'meta_key'       => 'grupo',
                                    'meta_value'     => $grupo_cliente

            );
                           $tmp = new WP_Query($args);

           if(!$tmp->have_posts()){
              echo 'não tem notícias no momento';
             return;
           }

          while ($tmp->have_posts()){
            $tmp->the_post(); 
            echo get_the_title().'<br>';
          }

0

I will assume that users (customers) when registering/registering on Wordpress, choose the group they will be part of and consequently this will save a new user metadata to the database.

To retrieve this information you were informed during registration, you will need to use the function get_user_meta then passing the metadata key, say for example that you chose to call it user_group_info, you then recover the value of user_group_info, perform the necessary checks and finally try to get the posts according to the value of user_group_info.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.