Custom Fields
Re-Envisioned
Support MB Relationships Filter Relationships Query

  • This topic has 4 replies, 2 voices, and was last updated 1 month ago by JC.
  • Creator
    Topic
  • #31549
    JC
    Participant

    Hello,

    I'm working on an events website. I have custom post types for venues and dates. The relationship is from the dates to the venues. I'm working on a query to show all dates from a venue single view. This one is working:

    $connected = new WP_Query( [
       'relationship' => [
       'id'   => 'fromdatestovenues',
       'to' => get_the_ID(),
      ],
      'nopaging'     => true,
     ] );

    How can I get only dates from the relationship where a custom select field ('wochentag_select') is set to a specific value?

    Best

    Jan

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #31555
    Long Nguyen
    Moderator

    Hi,

    You can add the meta query (field ID and value) to the arguments of the query to filter dates, please read more on the WP documentation https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

    #31556
    JC
    Participant

    Hi Long,

    thanks for your answer. I'm probably doing something wrong, because the result isn't filtered:

    $connected = new WP_Query( [
        'relationship' => [
        'id'   => 'fromdatestovenues',
        'to' => get_the_ID(),
        'meta_key'   => 'wochentag_select',
        'meta_value' => $dertag,],
        'nopaging'     => true,
    ] );

    doesn't show any posts:

    $connected = new WP_Query( [
        'relationship' => [
        'id'   => 'fromdatestovenues',
        'to' => get_the_ID(),
            ],
        'meta_key'   => 'wochentag_select',
        'meta_value' => $dertag,
        'nopaging'     => true,
    ] );

    Is it relevant that the custom field is not in the post I'm running the query from, but in the post with the relationship?

    In Views (Singleview) the working query is:

    {% for post in relationships.fromdatestovenues.from|filter(post => post.wochentag_select.name == dertag or post.wochentag_select.name == heute)|sort((a, b) => b.wochentag_select <=> a.wochentag_select ?: a.startzeit <=> b.startzeit) %}

    But I have to rewrite it to php.

    #31566
    Long Nguyen
    Moderator

    Hi,

    What is the value that you assign to the variable $dertag? Please assign a specific value to the argument meta_value to check if it works.
    I also see that in the View code, the query uses from instead of to in the relationship arguments, you can change to from and re-check it.

    On another note, adding the argument post_type to the query might help to specify a post type.

    $connected = new WP_Query( [
        'post_type' => 'dates', //here
        'relationship' => [
            'id'   => 'fromdatestovenues',
            'to' => get_the_ID(), // change to 'from'?
        ],
        'meta_key'   => 'wochentag_select',
        'meta_value' => 'abcd',
        'nopaging'     => true,
    ] );
    #31592
    JC
    Participant

    Hi,

    thanks for your answer. I tried your suggestions in every combination without any success. I guess there is another problem. I'll try to solve this with custom categories instead of relationships then.

    Best

    Jan

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.