Wordpress – Sort posts alphabetically by meta value












0














I have a custom post type of products, and each product belongs to a product category. I am trying to query the products to get all products sorted alphabetically by their product category i.e.

First:
Product: Lion

Category: Animals



Last:

Product: Snowmobile

Category: Winter



I am using a custom field for the product category, but my query doesn't sort them alphabetically - but instead by which date they are published. The product_cat_meta field is a regular text field set up in custom fields. Query is here:



function get_products()
{


$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}


The result of this query just returns the prod category in the way they are set up in wordpress – the latest posts first, but not sorted alphabetically










share|improve this question






















  • Have you tried using Category Parameters?
    – aagjalpankaj
    Nov 16 at 9:59










  • The product category is its own post type, so I don't have any category parameters available for the products unfortunately. Instead it is two custom fields where one links to the category page, and the other is just plain text of the category.
    – user2868900
    Nov 16 at 10:18










  • Are you sure that you are using right meta_key ? Because your query is correct.
    – shazyriver
    Nov 16 at 11:08
















0














I have a custom post type of products, and each product belongs to a product category. I am trying to query the products to get all products sorted alphabetically by their product category i.e.

First:
Product: Lion

Category: Animals



Last:

Product: Snowmobile

Category: Winter



I am using a custom field for the product category, but my query doesn't sort them alphabetically - but instead by which date they are published. The product_cat_meta field is a regular text field set up in custom fields. Query is here:



function get_products()
{


$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}


The result of this query just returns the prod category in the way they are set up in wordpress – the latest posts first, but not sorted alphabetically










share|improve this question






















  • Have you tried using Category Parameters?
    – aagjalpankaj
    Nov 16 at 9:59










  • The product category is its own post type, so I don't have any category parameters available for the products unfortunately. Instead it is two custom fields where one links to the category page, and the other is just plain text of the category.
    – user2868900
    Nov 16 at 10:18










  • Are you sure that you are using right meta_key ? Because your query is correct.
    – shazyriver
    Nov 16 at 11:08














0












0








0







I have a custom post type of products, and each product belongs to a product category. I am trying to query the products to get all products sorted alphabetically by their product category i.e.

First:
Product: Lion

Category: Animals



Last:

Product: Snowmobile

Category: Winter



I am using a custom field for the product category, but my query doesn't sort them alphabetically - but instead by which date they are published. The product_cat_meta field is a regular text field set up in custom fields. Query is here:



function get_products()
{


$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}


The result of this query just returns the prod category in the way they are set up in wordpress – the latest posts first, but not sorted alphabetically










share|improve this question













I have a custom post type of products, and each product belongs to a product category. I am trying to query the products to get all products sorted alphabetically by their product category i.e.

First:
Product: Lion

Category: Animals



Last:

Product: Snowmobile

Category: Winter



I am using a custom field for the product category, but my query doesn't sort them alphabetically - but instead by which date they are published. The product_cat_meta field is a regular text field set up in custom fields. Query is here:



function get_products()
{


$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}


The result of this query just returns the prod category in the way they are set up in wordpress – the latest posts first, but not sorted alphabetically







php mysql wordpress






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 at 9:36









user2868900

559




559












  • Have you tried using Category Parameters?
    – aagjalpankaj
    Nov 16 at 9:59










  • The product category is its own post type, so I don't have any category parameters available for the products unfortunately. Instead it is two custom fields where one links to the category page, and the other is just plain text of the category.
    – user2868900
    Nov 16 at 10:18










  • Are you sure that you are using right meta_key ? Because your query is correct.
    – shazyriver
    Nov 16 at 11:08


















  • Have you tried using Category Parameters?
    – aagjalpankaj
    Nov 16 at 9:59










  • The product category is its own post type, so I don't have any category parameters available for the products unfortunately. Instead it is two custom fields where one links to the category page, and the other is just plain text of the category.
    – user2868900
    Nov 16 at 10:18










  • Are you sure that you are using right meta_key ? Because your query is correct.
    – shazyriver
    Nov 16 at 11:08
















Have you tried using Category Parameters?
– aagjalpankaj
Nov 16 at 9:59




Have you tried using Category Parameters?
– aagjalpankaj
Nov 16 at 9:59












The product category is its own post type, so I don't have any category parameters available for the products unfortunately. Instead it is two custom fields where one links to the category page, and the other is just plain text of the category.
– user2868900
Nov 16 at 10:18




The product category is its own post type, so I don't have any category parameters available for the products unfortunately. Instead it is two custom fields where one links to the category page, and the other is just plain text of the category.
– user2868900
Nov 16 at 10:18












Are you sure that you are using right meta_key ? Because your query is correct.
– shazyriver
Nov 16 at 11:08




Are you sure that you are using right meta_key ? Because your query is correct.
– shazyriver
Nov 16 at 11:08












1 Answer
1






active

oldest

votes


















0














You can try to put the code in functions.php file.



The code, which can be dropped into your current theme’s functions.php if you like:






function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}





Get More details, follow the link: https://codex.wordpress.org/Alphabetizing_Posts






share|improve this answer























  • Thank you for the reply, however I am not using woocommerce so I don't think this will be applicable for this problem sorry.
    – user2868900
    Nov 16 at 10:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335041%2fwordpress-sort-posts-alphabetically-by-meta-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can try to put the code in functions.php file.



The code, which can be dropped into your current theme’s functions.php if you like:






function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}





Get More details, follow the link: https://codex.wordpress.org/Alphabetizing_Posts






share|improve this answer























  • Thank you for the reply, however I am not using woocommerce so I don't think this will be applicable for this problem sorry.
    – user2868900
    Nov 16 at 10:16
















0














You can try to put the code in functions.php file.



The code, which can be dropped into your current theme’s functions.php if you like:






function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}





Get More details, follow the link: https://codex.wordpress.org/Alphabetizing_Posts






share|improve this answer























  • Thank you for the reply, however I am not using woocommerce so I don't think this will be applicable for this problem sorry.
    – user2868900
    Nov 16 at 10:16














0












0








0






You can try to put the code in functions.php file.



The code, which can be dropped into your current theme’s functions.php if you like:






function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}





Get More details, follow the link: https://codex.wordpress.org/Alphabetizing_Posts






share|improve this answer














You can try to put the code in functions.php file.



The code, which can be dropped into your current theme’s functions.php if you like:






function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}





Get More details, follow the link: https://codex.wordpress.org/Alphabetizing_Posts






function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}





function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);

$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();

$prod_meta = get_field('product_cat_meta');
echo $prod_meta;

);
$index++;
} // end while
} // end if
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 at 10:45

























answered Nov 16 at 10:05









Priyanka Modi

9661511




9661511












  • Thank you for the reply, however I am not using woocommerce so I don't think this will be applicable for this problem sorry.
    – user2868900
    Nov 16 at 10:16


















  • Thank you for the reply, however I am not using woocommerce so I don't think this will be applicable for this problem sorry.
    – user2868900
    Nov 16 at 10:16
















Thank you for the reply, however I am not using woocommerce so I don't think this will be applicable for this problem sorry.
– user2868900
Nov 16 at 10:16




Thank you for the reply, however I am not using woocommerce so I don't think this will be applicable for this problem sorry.
– user2868900
Nov 16 at 10:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335041%2fwordpress-sort-posts-alphabetically-by-meta-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?