Support » Developing with WordPress » Help with PHP array

  • Resolved peebz

    (@peebz)


    Can someone please help me edit an array so that instead of displaying posts from one category ID (8), the array displays posts from either category ID 8 OR category ID 649?

    An example is on this page, under “Agents Listings”:
    http://bulldogrealtors.com/agents/jeff-curtis

    The page is currently displaying any of the Agent’s Listings (posts) with category ID = 8. Here is the code that works for the single category:

    <div class=”row-agent”>
    <?php $agent_name = get_the_title(); ?>
    <div class=”none”><?php
    query_posts(array(
    ‘cat’ => 8,
    ‘showposts’ => 4,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘agent’,
    ‘value’ => $agent_name,
    ‘compare’ => ‘LIKE’,
    )
    ),
    ));
    ?>
    </div>

    All I need to do is also show any of the Agent’s posts from category ID = 649, so that if the Agent has two posts with category ID 8 and two posts with category ID 649, the page would show all fours posts.

    It doesn’t matter if it first shows posts from category ID 8 and only then from category ID 649, I just need posts with category ID 649 to show if there are less than 4 posts with category 8.

    I hope this makes sense and I really appreciate the help.

    Best,
    P

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hey @peebz

    You can use this array.

    <div class=”row-agent”>
    <?php $agent_name = get_the_title(); ?>
    <div class=”none”><?php
    query_posts(array(
    ‘cat’ => '8,649',
    ‘showposts’ => 4,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘agent’,
    ‘value’ => $agent_name,
    ‘compare’ => ‘LIKE’,
    )
    ),
    ));
    ?>
    </div>

    You can add categories separated by comma.

    For more details you can check here. https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    Hi Chetan,

    Thanks so much for your response. I really appreciate it.

    First, the good news:

    On a different template, adding the 2nd category worked like a charm. When an Agent has a bunch of “ID = 8” listings, they all show, but as you can see for this agent, who has only one “ID = 649” listing, it’s rendering perfectly:

    http://bulldogrealtors.com/agent-properties/?agent=Jackie%20Baker

    Alas, that template uses $args = array( vs. query_posts(array(, as follows:

    <div class=”property-inner-bg”>
    <?php
    if(isset($_GET[‘agent’]) && $_GET[‘agent’] !=”) {
    $agent_name = $_GET[‘agent’];

    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;

    $args = array(
    ‘cat’ => ‘8,649’,
    ‘showposts’ => 30,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘agent’,
    ‘value’ => $agent_name,
    ‘compare’ => ‘LIKE’
    )
    ),
    ‘paged’ => $paged
    );
    ?>

    Per my original question on the original template (http://bulldogrealtors.com/agents/jeff-curtis), the code you provided using query_posts(array( doesn’t break the page, but it seems to be losing the “Agent” key or value. Because when I inserted the code you provided, it displays all of the “8” and “649” posts regardless of the Agent. It didn’t control for the Agent’s name.

    I’m not sure if this makes sense, and if it doesn’t, please don’t worry because I may be able to control the “8” vs. “649” by using different templates all together. But if it’s an easy fix for you, do let me know!

    Thank you again, very kind of you to help.
    P

    Have you stored Agent name in “agent” meta?

    Hey Chetan,

    Thanks again but I’m afraid it’s above my head. I’m more of a graphic designer than coder, just trying to change this one aspect of templates developed years ago.

    (Though I will say this: I worked with .asp years ago, and it was much simpler to configure conditional statements!)

    Bottom line, I’ll mickey mouse something, and sincere thanks again for your help.

    P

    @peebbz

    I am talking about Custom Fields https://codex.wordpress.org/Custom_Fields. Without custom fields meta query will not work.

    Hey Chetan,

    It took me all day, but I finally got it! Including new dropdown properties driven by the new category.

    I just kept searching the site files, and the web, until I finally figured it out.

    Thank you very much. Good Karma for you ~

    P

    @peebz Great to see everything working for you. 🙂

    Thanks again.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Help with PHP array’ is closed to new replies.