0

I have this list of posts in wordpress. I've made a favorite function that when you mark something as favorite it gets added to the front of the line. The issue here is I run this 2 place. One top panel which shows my favorite posts that I'm attached to and then the second post that shows my favorized posts from other users. Top panel works just fine.. But the issue is that my profile cards are shown in the second panel where other users cards are shown: https://gyazo.com/75cf45a0844cba1f9ca6818e059d8bf6

As you can see... I have a duplicate in the second panel. How do I get rid of it using WP_Query?

Users are inside a multiple user field with ACF Fields.

I've tried doing all kinds of stuff such as AND and ORs. I am mostly likely close to the solution, but I can't seem to find the solution..

TOP Panel code:

$aProjectArgs = array(
        'post_type' => 'project',
        'post_status'=> 'publish',
        'posts_per_page'=> -1,
        'orderby'   => 'favorit_users',
        'order'   => 'DESC',
        'meta_query' => array(
            'relation' => 'OR',
                array(
                    'relation' => 'OR',
                    array(
                        'key' => 'project_users',
                        'value' => get_current_user_id(),
                        'compare' => '='
                    ),
                    array(
                        'key' => 'project_users', // name of custom field
                        'value' => '"'.get_current_user_id().'"',
                        'compare' => 'LIKE'
                    ),
                ),
                array(
                    'relation' => 'AND',
                    array(
                        'relation' => 'OR',
                        array(
                            'key' => 'project_users',
                            'value' => get_current_user_id(),
                            'compare' => '='
                        ),
                        array(
                            'key' => 'project_users', // name of custom field
                            'value' => '"'.get_current_user_id().'"',
                            'compare' => 'LIKE'
                        ),
                    ),
                    "favorit_users" => array(
                        'key' => 'favorite_users',
                        'value' => get_current_user_id(), 
                        'compare' => 'LIKE'
                    ),
                ),
            ),
        );
    $aProjectQuery = new WP_Query($aProjectArgs);

Second panel (LOWER):

$allProjectArgs = array(
        'post_type' => 'project',
        'post_status'=> 'publish',
        'posts_per_page'=> -1,
        'orderby'   => 'favorit_users',
        'order'   => 'DESC',
        'meta_query' => array(
            'relation' => 'OR',
                array(
                    'relation' => 'OR',
                    array(
                        'key' => 'project_users',
                        'value' => get_current_user_id(),
                        'compare' => '!='
                    ),
                    array(
                        'key' => 'project_users',
                        'value' => '"'.get_current_user_id().'"',
                        'compare' => 'NOT LIKE'
                    ),
                    array(
                        'key' => 'favorite_users',
                        'compare' => 'NOT EXISTS'
                    ),
                ),
                "favorit_users" => array(
                    'key' => 'favorite_users',
                    'value' => $iCurrentUserID, 
                    'compare' => 'LIKE'
                ),
            ),
        );
    $allProjectQuery = new WP_Query($allProjectArgs);

In the end the duplicate should be hidden in the second panel. Pretty much every card I am attached to should be hidden. Cards with my favorite (stars) should stay IF it is not related to my user.

1

Hopefully i understood correctly what you want to achieve here.

If you want to exclude all posts that contain get_current_user_id() in the project_users ACF repeater, but at the same time contains has get_current_user_id() in favourite_users, the correct meta query looks like this:

'meta_query' => array(
    'relation' => 'AND',
        array(
            array(
                'key' => 'project_users',
                'value' => '"'.get_current_user_id().'"',
                'compare' => 'NOT LIKE'
            ),
        ),
        "favorit_users" => array(
            'key' => 'favorite_users',
            'value' => '"'.get_current_user_id().'"',
            'compare' => 'LIKE'
        ),
    ),
);

Note that unlike in your example, i'm using 'relation' => 'AND' so that both conditions have to be met.

Also, note that multiple values ACF select fields are stored serialized in the db. That means the real value stored in the database looks like this:

| meta_id | post_id | meta_key      | meta_value         |
|---------|---------|---------------|--------------------|
| 14      |       8 | project_users | a:1:{i:0;s:1:"1";} |
| 20      |      11 | project_users | a:1:{i:0;s:1:"2";} |

Which means that the compare should always be LIKE or NOT LIKE and the value should always be quoted in your case, because there will always be numbers otherwise (s:1 for string 1 character long etc.). The values you are looking for (user ids) on the other hand will be stored with quotes.

Here is the full query i ended up with:

$allProjectArgs = array(
    'post_type' => 'project',
    'post_status'=> 'publish',
    'posts_per_page'=> -1,
    'orderby'   => 'favorit_users',
    'order'   => 'DESC',
    'meta_query' => array(
        'relation' => 'AND',
            array(
                array(
                    'key' => 'project_users',
                    'value' => '"'.get_current_user_id().'"',
                    'compare' => 'NOT LIKE'
                ),
            ),
            "favorit_users" => array(
                'key' => 'favorite_users',
                'value' => '"'.get_current_user_id().'"',
                'compare' => 'LIKE'
            ),
        ),
    );
$allProjectQuery = new WP_Query($allProjectArgs);
8
  • Thanks for a very good informed answer.. Sadly it did not work. I ended up with no posts. We have tried some stuff and found that this: pastebin.com/qhb0kkdK does the job, however.. It doesnt sort with favorites being the firsts posts. Got any ideas? – Lato Sep 3 '19 at 13:09
  • Maybe you could better debug the query if you saw it as SQL query. Could you try printing the query: $allProjectQuery = new WP_Query($allProjectArgs); var_dump($allProjectQuery->request); – rozklad Sep 3 '19 at 13:15
  • Of course. Here's the response: pastebin.com/aC0r97eW – Lato Sep 3 '19 at 13:17
  • And the two ACF fields are project_users and favorite_users, projects are status 'publish' and other things seem correct about the query? I made a testing installation with query like this pastebin.com/FwVV0B4k and the results are as expected imgur.com/a/YPLAhPP – rozklad Sep 3 '19 at 13:30
  • Everything should be as is. We have almost the same code somewhere else which we tried to re-use, that did not work so we had to make a little twist in the code. Atm we are both lost. – Lato Sep 3 '19 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.