Query_post , pick up taxonomy by custom post field

Asked

Viewed 324 times

0

Good morning to you all. A one page I have a custom field with the name of a post_type . In query_post you want to take the value that is not custom field with the name (choose)

the $choice is the amount we take in the custom field. inserir a descrição da imagem aqui

I don’t know if you can understand my question, but I appreciate your help

  • do not use query_posts -> https://codex.wordpress.org/pt-br:Refer%C3%Aancia_de_fun%C3%A7%C3%A3o/query_posts

2 answers

2

If I understood your question, because it is very strange, it seems that you want to redeem the value of the meta field, for that, would be enough the following code.

<!-- Quando está dentro do loop de posts e possui o post alimentado corretamente -->
<?php $escolha = get_post_meta( get_the_ID(), 'escolha', true ); ?>


<!-- Quando está fora do Loop ou precisa passar o ID manualmente -->
<?php $escolha = get_post_meta( "ID_DO_POST", 'escolha', true ); ?>

If you want to rescue all the meta Fields, you can hide the field, so Wordpress will return all.

<!-- Caso não esteja dentro do Loop, passe o ID do post no lugar de get_the_ID() -->
<?php $metas = get_post_meta( get_the_ID() ); ?>

Now, if you want to redeem all posts with a certain meta field and certain value:

$args = array(
   'meta_query' => array(
       array(
           'key' => 'escolha',
           'value' => 'valor_do_seu_meta_campo',
           'compare' => '=',
       )
   )
);

$query = new WP_Query($args);

With the category name at hand, you can redeem the posts as follows:

$category = get_post_meta($post->ID, 'escolha', true);

$query = new WP_Query(
    array(
        'category_name'  => $category,
        'orderby'        => 'title',
        'posts_per_page' => 3,
    )
);
  • Hello Bruno This out of the loop, I did the test to get the value of the field like this: $Category = get_post_meta($post->ID, 'choice', true) echo $Category (the value appears without problem) Then goes query_post query_posts('$Category=category-all-family & showposts= -1 &showposts= 3 & orderby=title'); PS: I’m still trying to figure out how to put the code in the question

  • I added at the end how to pick up posts with the category Slug.

  • I had already tried it this way, but query_post does not take the value

  • Is your category’s Slug exactly the same? If you have space you won’t find.

  • Instead of having it at the beginning ( ? ) should have ( " ) query_posts("$Category=category-all-family & showposts= -1 &showposts= 3 & orderby=title") Thanks for the help

0

Instead of having at the beginning ( ? ) should have ( " )

query_posts("$category=categoria-todas-familia & showposts= -1 &showposts= 3 & orderby=title")

Browser other questions tagged

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