• Hi, I have a bunch of pages published on my local wordpress setup.

    Lets say for a simple example, each page contains the standard the_title(), the_content() and 1 custom field:- get_field(‘section’) (Im using the advanced custom field plugin).

    I want to create a kinda of index page, which lists all the published pages and groups them based on the custom field value of ‘section’.

    So, I have say 4 pages assigned to a section called ‘Oranges’
    3 pages with section called ‘Apples’
    10 pages with section called ‘Pears’

    Im trying to setup a shortcode within function.php and using WP_Query to loop through through all the ‘Oranges’ pages, and list them together, then loop through all the ‘Apples’ pages, and so on and so forth.

    How can I achieve this with just one shortcode ? Currently Im no using categories if that helps at all.

    Any info much appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You can pass WP_Query custom_field parameters: https://codex.www.ads-software.com/Class_Reference/WP_Query#Custom_Field_Parameters

    Was that not working? What do you have so far?

    Thread Starter MrJLC

    (@mrjlc)

    Yeah, Ive already tried the custom field within WP_Query, but cant seem to fathom that method out to get the results I want. Since creating this thread, Ive done a bit more searching and found this within the codex:

    https://codex.www.ads-software.com/Displaying_Posts_Using_a_Custom_Select_Query

    Havnt tried it yet, but will do soon. Hopefully it will work out(!)

    Thread Starter MrJLC

    (@mrjlc)

    Just an update, I managed to get the results I wanted by doing a double WP_QUERY:

    function getResults() {
    //first, lets get all the items within the ‘groceries’ section, regardless of what type of product they are
    $args = array(
    ‘post_type’ => ‘page’,
    ‘post_status’ => ‘publish’,
    ‘paged’ => $paged,
    ‘posts_per_page’ => ‘-1’,
    ‘meta_query’ => array(array(‘key’ => ‘section’,’value’ => ‘Groceries’,’compare’ => ‘LIKE’)),
    );

    $link = new WP_Query($args);

    $types = array ();

    //loop through all the products and populate the ‘types’ array
    while($link->have_posts()) : $link->the_post();
    $type = get_field(‘type’);
    $types[] = $type;
    endwhile;

    //strip out the duplicates
    $result = array_unique($types);

    //now loop through the size of the ‘types’ array, running a fresh query, but this time, filtering the results by ‘type’
    foreach ($result as $value) {
    $args = array(
    ‘post_type’ => ‘page’,
    ‘post_status’ => ‘publish’,
    ‘paged’ => $paged,
    ‘posts_per_page’ => ‘-1’,
    ‘meta_query’ => array(array(‘key’ => ‘type’,’value’ => $value,’compare’ => ‘LIKE’),array(‘key’ => ‘section’,’value’ => ‘Groceries’,’compare’ => ‘LIKE’)),
    );

    $link = new WP_Query($args);
    //display the title based on the type of product we are showing
    echo ‘<h2>’.$value.'</h2>’;
    // the include file is a neatly formatted table that loops through all the matching types of products.
    include (“include_results_table.php”);
    }
    }
    add_shortcode(‘getResults’,’getResults’);

    So, it works at least. Perhaps not the most efficient method, but the script wont be handling massive amounts of data.

    But if anyone has a cleaner/simpler approach, feel free to post ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_Query Group By Help!’ is closed to new replies.